note 38768 added to language.variables.external

From: Date: Tue, 06 Jan 2004 17:03:37 +0000
Subject: note 38768 added to language.variables.external
Groups: php.notes 
Request: Send a blank email to php-notes+get-63008@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/language.variables.external.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38768 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38768&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38768&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63008) next »