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.
<?php
//if the variable is "variable"
$value = "<script language='javascript'>document.write(variable);</script>";
?>
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.
Last edited by ATBS; 05-11-2005 at 01:22 PM.
Reason: wrong item quoted
5. How can I pass a variable from Javascript to PHP?
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.
PHP Code:
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />\n";
echo "Screen height is: ". $_GET['height'] ."<br />\n";
} else {
// pass the geometry variables
// (preserve the original query string
// -- post variables will need to handled differently)
After you should be able to php unserialize the data you are passing.
I have almost completed implimenting it in my code. (so unless I didn't catch what you where asking...)
Bookmarks