If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Passing Data from Javascript to PHP

I've been working hard for quite sometime to pass the value from a javascript text box to a php page. I've tried with all he conventional method but was unable to parse the data. I've attached the code with this post. Plesae see what went wrong with this.

But remember, php is serverside while javascript is clientside. I think the most conventional method would be to do something along the lines of the following:

PHP Code:

<?php
if (!$xyz)
{
?>
<script language="javascript">
//lets say the variable is "xyz" and the value is "15"
//you already have "xyz" and you want to pass it to php, so it would be "$xyz"
location.href="<?php echo $PHP_SELF; ?>?xyz=15";
</script>
<?php
}
?>

<?php
//if you are using a newer version of php (i use php 5), you don't need to do anything but use $xyz. Otherwise, you would need to register it.
//use $xyz as needed.
?>

Unfortunately, the first suggestion doesn't work. The second would require a refresh. I am also investigating the passing of a variable from Javascript to PHP. If I come up with something I'll post it. It hasn't been a priority yet.

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables.

It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side.