I want to store the value of x in js variable into php variable on the same page.
Asked
Active
Viewed 59 times
-3
-
why ? you want to store client side input to server side. you can store js value into hidden input and then you can use it in php – devpro Jul 18 '19 at 08:48
-
2Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) <- When reading that post, remember that Javascript is client side and PHP is server side. – M. Eriksson Jul 18 '19 at 08:48
2 Answers
0
Short answer is: you can not.
The reason for this: PHP is executed on the server side, and the response is sent to the browser, and (usually) generated as HTML. The javascript is rendered after this, so at this point, the PHP code no longer exists.
Long answer:
You can send a javascript variable to PHP using an XHR request (or more commonly known as an AJAX call). This will be a different request from the one that loads your initial page though. For more information see this: https://www.w3schools.com/Php/php_ajax_php.asp

Erik Baars
- 2,278
- 1
- 8
- 14
0
you can use ajax or add input hidden with this value and when submit form sent a value
i think in most cast ajax in best.
$.ajax({ url: 'url', method: 'POST',
data: 'data',
cache: false,
success:function(data){
},
error: function(data){
}
}); // end ajax