Fwd: [PHP] php and js variables
| From: | Morgan Doocy | Date: | Wed, 07 May 2003 16:14:08 +0000 |
| Subject: | Fwd: [PHP] php and js variables | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-146701@lists.php.net to get a copy of this message | ||
Sent this to the author earlier, but accidentally didn't reply to list (no reply-to header). Forwarded for archive reference.
Morgan
Begin forwarded message:
From: Morgan Doocy <mdoocy@mac.com> Date: Sun May 4, 2003 1:47:20 AM US/Pacific To: Paul Marinas <paulm@rdscv.ro> Subject: Re: [PHP] php and js variables Because JavaScript is client side, the only way to interact with PHP is via a form submission. If you have a form element (perhaps a hidden one), you can write the JavaScript variable to it and submit the form: <form name="frm" action="<?= $_SERVER["SCRIPT_NAME"]; ?>" method="get"> <input type="hidden" name="foo" /> </form> ... <script type="text/javascript> document.frm.foo.value = "bar"; document.frm.submit(); </script> Or, you could just use JavaScript to tack on a query string: <script type="text/javascript" location.href += "?foo=bar"; </script> Either way, once the JavaScript is executed, the page will be reloaded with a query string tacked on. This is read using the $_GET superglobal: <? if (isset($_GET) && array_key_exists("foo", $_GET)) { $FOO = $_GET["foo"]; // Now you can use the variable $FOO, which contains // the value which was passed through the query string // from the JavaScript. } ?> It's important to keep in mind though that JavaScript and PHP are handled completely separately - you can't have scripts of both kinds interact within the same document, because they are executed separately. Morgan On Saturday, May 3, 2003, at 11:27 PM, Paul Marinas wrote:Is there a way to get the value of a javascript variable and put it into a php variable? Thanks Paul. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php