-2

EDIT: thanks for clearing up my confusion regardign PHP and Javascript. Got to use AJAX.

I'm just messing around trying to do a basic write to file using PHP/Javascript

I have a script like

<head>
  <?php
      function writeToFile($file, $data) {
          file_put_contents($file, $data);
      }
  ?>
  <script>
      function funct() {
          <?php writeToFile('text.txt', 'hello'); ?>
      }
      window.onload = funct;
  </script>
</head>

Unfortunately, text.txt is empty. What am I missing?

Smipims
  • 343
  • 1
  • 8
  • 20

4 Answers4

5

You are confusing server side languages with client side languages.

<?php
function writeToFile($file, $data) {
    file_put_contents($file, $data);
}

    writeToFile('text.txt', 'hello'); 

?>

The above will work.

tags are for javascript (which works on the client side - i.e. in the browser)

To call your script in PHP from a web page you need to look at AJAX - but I would start with learning a bit about client vs server side languages first - it can be confusing until it clicks!

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • Oh ok. I'm very inexperienced with PHP so I pulled that syntax from one of the answers here: http://stackoverflow.com/questions/15757750/php-function-call-using-javascript The reason I used PHP and Javascript is that I want to get the browser's user agent string. Guess I need to use AJAX – Smipims Mar 28 '14 at 17:34
  • Not a problem - I still sometimes get confused with stuff (wait until you get to JSON - that takes a little while to get your head around!) - anything inside `` happens on the server - the broswer knows nothing about this until you use `echo` or similar. Same goes for javascript - that happens on the browser and the server knows nothing about it unless you `POST` items to the server (tell the server some information) - have a search for 'server side vs client side langauges' on google - hopefully it will help! – GrahamTheDev Mar 28 '14 at 17:38
1

You should use fopen to get a file handle and then use fputs to write to that file. On top of that, you can't just use javascript to execute php commands.

It works as follows:

The user requests a .php file from the server. The server executes the php code in that file and writes it output as HTML in the rest of the script. Since Javascript is executed on the clientside, the php stuff is already done by the server. So what your client actually gets is:

<head>

  <script>
  function funct() {

  }
  window.onload = funct;
  </script>
</head>

If you want to execute some php functions while the user is doing input and don't want to reload the page, you can check ajax. But for your purpose i think you should read about fopen, fputs and so on.

http://www.php.net/manual/de/function.fopen.php http://www.php.net/manual/de/function.fputs.php

ntx
  • 9
  • 2
1

You can't use PHP and javascript together like that. You should use AJAX. So rewrite the funct() like this:

function funct() {
  var xmlhttp = new XMLHttpRequest();

  xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          alert('Writing completed!');
      }
  }

  xmlhttp.open("GET", "script.php", true);
  xmlhttp.send();
}

Note the xmlhttp.open("GET", "script.php", true); part. Yu must create new file to your server called script.php and add PHP contents of your script to it(writeToFile() function, function call)

aksu
  • 5,221
  • 5
  • 24
  • 39
1

Your code will not works due to the difference in execution of php and javascript. The php (server side) will be execute first and will execute both

  <?php
     function writeToFile($file, $data) {
         file_put_contents($file, $data);
     }
  ?>

AND

   <?php writeToFile('text.txt', 'hello'); ?>

from your code and the html result will be send to the client side. And guess what ? the code which will be present to javascript is the following

<head>
  <script>
      function funct() {          
      }
      window.onload = funct;
  </script>
</head>

So if the file text.txt is on the client side then you need to use ajax to handle this. Here is a good introduction to Ajax

good luck

Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82