Send JavaScript variable to PHP variable
Solution 1
As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name']
after your javascript was executed.
Solution 2
PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];
Solution 3
It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.
In other case, if this will happen when a user submits a form, you can send the variable in an hidden field or append ?variableName=someValue"
to then end of the URL you are opening. :
http://www.somesite.com/send.php?variableName=someValue
or
http://www.somesite.com/send.php?variableName=someValue&anotherVariable=anotherValue
This way, from PHP you can access this value as:
$phpVariableName = $_POST["variableName"];
for forms using POST method or:
$phpVariableName = $_GET["variableName"];
for forms using GET method or the append to url method I've mentioned above (querystring).
mrbunyrabit
Updated on July 23, 2022Comments
-
mrbunyrabit almost 2 years
First I thought that I had to convert JavaScript to PHP, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable
<script type="text/javascript"> function scriptvariable() { var theContents = "the variable"; } </script>
to a PHP variable
<?php $phpvariable ?>
That function in the JavaScript executes when let's say I click on a button.
Now I have no idea on how to assign that phpvariable to the JavaScript one to use the phpvariable to look up stuff in my database. I know I can add it to my url or some thing and refresh the page, but I would like to do it with AJAX as I might have to use this Ajax method further in my webpage.
So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?
-
mrbunyrabit over 12 yearsI know im doing something wrong.... I just dont know what... This is how i inserted this code
<script type="text/javascript"> var variableToSend = 'foo'; $.post('file.php', {variable: variableToSend}); </script>
` <?php $variable = $_POST['variable']; ?>` ` then i get this error that it is an undefined variable... Notice: Undefined index: variable in (the php bracket) any idea what im doing wrong? -
Bryan over 12 years@mrbunyrabit You need to include the jQuery library in your page... otherwise the "$" variable, used by jQuery, won't be defined. Look here under "complete example" to see how to include it in your page. docs.jquery.com/Tutorials:How_jQuery_Works.
-
Alex Wayne over 12 yearsTo be fair, doing AJAX without some library like jQuery a path lined with death and despair.
-
rlemon over 12 yearsThe easy approach is often also the incorrect approach.
-
Zirak over 12 years@Squeegy Simple ajax transactions take roughly 6 lines of code
-
PeeHaa over 12 yearsfor requiring the OP to 'install' and use jQuery for a simple AJAX call.
-
Ricardo Souza over 11 yearsWhy not? Try to explain to a new user how to do a crossbrowser AJAX call in pure JavaScript in less than a page.