Jump to content

Assign Javascript variable to PHP Variable


123456

Recommended Posts

You will need to refresh your page and send the value of selection option in the URL. This can be achieved through javascript :

 

var selected_value = document.your_form.select_name.options[document.your_form.select_name.options.selectedIndex].value;
location = 'abc.php?id='+selected_value;

 

Now on php page you can use php code:

<?php
echo $_GET['id'];
?>

Javascript is exclusively on the client end (the users computer) and PHP is on the server end exclusively (the server itself) and they do not interact backwards -- that is, the server sends scripts to the client, but not the other way around.

 

You either have to use the javascript URL method posted above, or use ajax, where you're making a call using that URL method, but only refreshing a <div> or <span>, not the entire page.  If you have to have it with no page refreshes, then you need to use Ajax.

 

Check out the Apress book "Beginning Ajax with PHP From Novice to Professional" ... it explains how to create Ajax objects and how to use them with PHP to query databases and such.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.