How can I store the output of a JavaScript variable in a PHP variable?
I have the following code in JavaScript:
<script> var a="Hello"; </script> PHP Code: -
<?php $variable = // I want the above JavaScript variable 'a' value to be stored here ?> Note. I don't want it to be in shape. I have some logic in JavaScript and I want to use it on the same PHP page ... Please let me know how I can do this.
You must remember that if JS and PHP live in the same document, PHP will be executed first (on the server), and JS will be executed second (in the browser) - and they will NEVER interact (except where you output JS with PHP, which is not really an interaction between the two engines).
With this in mind, the closest you can use the PHP variable in JS is:
<?php $a = 'foo'; // $a now holds PHP string foo ?> <script> var a = '<?php echo $a; ?>'; //outputting string foo in context of JS //must wrap in quotes so that it is still string foo when JS does execute //when this DOES execute in the browser, PHP will have already completed all processing and exited </script> <?php //do something else with $a //JS still hasn't executed at this point ?> As I said before, in this scenario, PHP (ALL) executes FIRST on the server, calling:
- PHP variable
$ashould be created as the string 'foo' - the value of
$ais output in the context of some JavaScript (which is not currently running) - more done with PHP
$a - all output, including JS with the assignment var, is sent to the browser.
As written, this leads to the following being sent to the browser for execution (I just deleted the JS comments):
<script> var a = 'foo'; </script> Then, and only then, JS will start execution with its own variable a set to "foo" (at this point, PHP is not displayed).
In other words, if they live in the same document and no additional interaction with the server is performed, JS cannot cause any effect in PHP. Furthermore, PHP is limited in its effect on JS on the simple ability to output some JS or something in the context of JS.
The ideal method would be to pass it by calling AJAX, but for the quick and dirty method all you have to do is reload the page using this variable in the $_GET parameter -
<script> var a="Hello"; window.location.href = window.location.href+'?a='+a; </script> Your page will reload and now in your PHP you will get access to the variable $_GET['a'] .
<?php $variable = $_GET['a']; ?> If this involves submitting the form, use the hidden input on the form and change the value of the hidden input to this variable value. Then you can get this hidden input value on the php page and assign it to the php variable after submitting the form.
Update:
According to your editing, you don't seem to understand how javascript and php work. Javascript is the client language, and php is the server language. Therefore, you cannot execute javascript logic and use this variable value for the php variable when executing the corresponding page on the server. You can run the appropriate JavaScript logic after the clientβs browser processes the web page returned from the web server (which has already executed the PHP code for the corresponding page). After executing the javascript code and after assigning the appropriate value to the corresponding javascript variable, you can use form submission or ajax to send the value of the javascript variable for use by another php page (or request to process and get the same php page).
You really can't. PHP is generated on the server and then sent to the browser, where JS begins to do this. So, no matter what happens in JS on the page, PHP does not know, because it has already done this. @manjula is correct that if you want this to happen, you would need to use POST or ajax.