hi friends can some one tell me How do I assign javascript variable to php variable? I am actually getting a value through javascript and I want the javascript values to be assigned to php variables. With out submitting the form.
views:
110answers:
5You need to send an AJAX request containing the information to a separate PHP script that processes the data.
You can assign PHP variable to a JavaScript one:
var some = <?php echo $other; ?>
But you can't do this backwards - it is a matter of that php is run BEFORE javascript. ;]
Unless you are doing something really esoteric (such as running server side JS and PHP):
The PHP runs, generates a response, sends it to the browser. If that response includes JavaScript then that JS will then be executed.
You can't get data from the JS back to the PHP without issuing a new HTTP request.
This could be done by setting document.location
, adding an <img>
element to the document with the data passed via the src
attribute (in both cases including the data in a query string, posting a <form>
to an <iframe>
, using an XMLHttpRequest object and a host of other methods.
It really depends on what you want to achieve.
Best way will be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but be aware that you will only be able to bring back a variable or store the data server side via a db or text file.
what do you want to do with the data that is sent to the server?