|
|
|||||
Passing JavaScript Value to PHPRecently, I received an email from a regular reader of WebDev, ‘nurmala.’ He/She wanted to know how you can pass a value written in Javascript to a PHP Variable. Before I get into the neat details, let me give you a quick overview of the main differences between Javascript and PHP. Javascript is a client scripting language. Whatever you code in Javascript will work only in the web browser of the user. So, if the user turns off Javascript in his web browser, your website will basically just drop dead. PHP is a server side scripting language. Now, what this does is, it runs in the server. So, the user can’t really turn it on or off, but you can, because you have control over the server script. Can they work together? Definately!
There are two main methods used to pass data between Javascript and PHP. A Javascript function will need to execute a PHP Script directly behind the scenes with the data or you can use javascript to store the value in a hidden field, and after the form is submitted to a PHP page, simply retrieve the data. You can see a demo of this method here: http://www.web-development-tutorials.com/demo/HowToPassValueBetweenPHPnJS/classic/ classic.html 1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> function SetJsValue() </script> <body> Just save the above code as classic.html. And let me explain what is going on in here. Before the </head> tag, I declared my Javascript variable called MyJavascriptVariable. Then, you will see the JavaScript function SetJsValue(). This function is executed after the submit button is clicked on the form. And in the actual HTML form itself I have a hidden field called ‘jsvalue.’ After the submit button is clicked, the SetJsValue() kicks in, it sets the value of the hidden field to whatever value is in the ‘MyJavascriptVariable’ and submit the whole form to the action.php page. action.php 1. <?php <?php if(isset($_POST['submit'])) ?> All action.php page is doing is retrieving the HTML input fields from the $_POST superglobal variable, and this way it gets access to the value of the javascript value. If you know about PHP Sessions, then you know how powerful and useful it is to store data for some period of time. Here’s a very cool trick you can use to pass data stored in a JavaScript variable to a PHP Page. modern.html 1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> $(document).ready(function() <body> In this basic HTML Web page, I included the jQuery library. jQuery is a Javascript library you can use to speed things up with Javascript. And it makes making AJAX calls super duper easy. You can download jQuery from http://jquery.com/ 1. var MyJavascriptVariable = 'Kate Winslet'; var MyJavascriptVariable = 'Kate Winslet'; $(document).ready(function() The variable MyJavascriptVariable is just a normal javascript variable with the value ‘Kate Winslet’. Next, using jQuery I am posting the value to ajax.php page only after the HTML document is fully loaded. And behind the scene, the ajax.php page will take the value and store it in a PHP Session. So, when the user visits sample-php-page.php, the value is already set. ajax.php 1. <?php <?php session_start(); //start PHP Session $_SESSION['jsvalue'] = $_POST['js']; //store the posted value in a php session variable ?> sample-php-page.php 1. <?php <?php source: web-development-tutorials |
|||||
| Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy |