简体   繁体   中英

PHP and Javascript interaction

I need some things clarified here:

I have a website that updates the values of two variables each time the site is reloaded in the browser. This page is obviously an HTML page but the variables are updated by javascript functions. This page runs on my server.

Can PHP retrieve the values of these variables and, using them, write them to a txt file or XML file? I realise javascript is a browser side language and PHP a server side language but I don't know how the two can relate properly in this context.

Also I don't have the option of recreating the javascript code as PHP script.

Is what I want to do possible?

Thanks

I am not sure when the variables are updated (if they are updated when the page is being generated, then why don't you just do it with PHP only?). But if they are updated on the client side, based on some actions made by the user, then just do the following:

  1. After the JS variables are updated, issue AJAX call to the server, sending the data ,
  2. On the server side, generate TXT or XML file using PHP, based on the data sent .

The 1st can be done using eg. jQuery's .post() method , and the second can be done by reading $_POST variable from within called PHP script (the one to which you send the data using AJAX), and by outputting it to TXT file (eg. by using PHP's fwrite() function ).

EDIT : There is a great reference about AJAX. See Mozilla Developer Network article on Asynchronous JavaScript + XML (AJAX) .

You want AJAX. Here is a good introduction to it, if you follow the next chapter, it gives you an example with PHP:

http://www.w3schools.com/php/php_ajax_intro.asp

The variables arent set yet when the PHP script runs so that wont work. You can do an AJAX call once the javascript script function gets the values you need and sends the values to a .php script to run

AJAX is the answer. You can use the XMLHttpRequest object directly or use a library like jQuery for the request. Then, you can send the data using a HTTP-POST-Request to your server, in this case your PHP script which then saves the values it receives in the $_POST array.

With jQuery, this can be done like this:

var data = {foo: 'bar', lorem: 'ipsum'};
$.post('http://example.com/save.php', data);

SLaks is right. You want to look into ajax. The easiest way would be to include the latest jquery library and then send the variables to your php script on load.

Latest jQuery 1.x library

jQuery ajax documentation

PHP fwrite documentation

The PHP fwrite docs will explain how to write to a flat text file.

Hope it helps. If you need clarification let us know.

javascript and php can have relation with each other in mentioned way : 1_send array from php to javascript

<script type="text/javascript">
<?php
  $myVar = array('a' , 'b' , 'c');
?>
   console.log(<?php echo json_encode($myVar); ?>);

</script>

2_ send and json file from php to javascript. (may be ajax call ) 3_ send xml file from php to javascript. (ofcourse json is preferred) 4_ use javascript with REST API to read from files or databse or ... which database had some thing with them.

and maybe some other ways.

depends on your need to use which one.

PHP and JS don't talk to each other. They have an intermediary...namely the HTML page.

PHP -> generates HTML -> sent to browser -> rendered -> JS interacts with html -> sent back -> PHP interacts with server response

Use javascript to retrieve these variable values , then send them to your php script using AJAX (in a json format) then your php code can retrieve these variables and write them to a file.

See this for an example.

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