简体   繁体   中英

Writing data to a server file using javascript + php

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?

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 - ie 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!

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM