note 38768 deleted from language.variables.external by nlopess
| From: | nlopess@php.net | Date: | Wed, 07 Jan 2004 14:31:52 +0000 |
| Subject: | note 38768 deleted from language.variables.external by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-63076@lists.php.net to get a copy of this message | ||
Note Submitter: mkk@queenheaven.com
----
I have found that a common problem at the moment seems to be passing variables from JavaScript to
PHP without reloading the page, but there is a simple and easy solution here - cookies.
If you set a cookie in JavaScript with expiry date null, you can easily retrieve it using $_COOKIE
in PHP. There is naturally the danger that the user's browser won't be cookie enabled, but
I guess that's a risk you take with JavaScript anyway.
<script name="JavaScript">
function setCookie(name, value) {
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
var jscriptvariable = "<br>How are you?";
setCookie('cookiename', jscriptvariable);
</script>
<?php
phpvariable = $_COOKIE['cookiename'];
//And the variable is saved as $phpvariable
print $phpvariable;
?>
It's a fair bit of code for one piece of information, but of course you only need the setCookie
function once and it saves the user the time & trouble of using forms or other methods of
passing the information on.
For a more detailed tutorial www.queenheaven.com/scripting
Hope you find this useful,
mKK