AJAX - With PHP Variables


AJAX - With PHP Variables

Passing variables in a URL

  • In php programs, passing of variables from one page to another is performed via page URL.

Passing Variables across Pages in php :

  • URLs with variables appear as shown below :

http://wikitechy.com/page.php?id=54

  • What is the purpose of a question mark with id value 54?
  • The reply will indicate that it is http query string characters that are present after the question mark. A http query string holds both variables and their values. 
  • The sample given above contains http query string with a variable named "id" and the value "54".

Here is another example :

http://wikitechy.com/page.php?name=Venkat

means, that the variable "name” has value as "Venkat".

Accessing the Query String Variable Inside php code :

  • Let's say you have a PHP page named people.php. Now you can call this page using the following URL :
people.php?name=Venkat		
  • With PHP, the variable 'name' can be accessed using the below code :
$_GET["name"]	 
  • $_GET – used to access the content from named query string variable.

Multiple Query String Variables

  • Multiple ampersand separated values are passed with query string as follows :
people.php?name=Venkat&age=30	
  • The above URL’s query string accustoms two variables: name and age. Each variable is accessed via a separate $_GET as follows :
$_GET["name"]         
$_GET["age"]
ajax get example with multiple query strings :
<html>
    <head>
        <title>Query string</title>
    </head>
    <body>
        <?php
            // The value of the variable name is found
            echo "<h1>Hello " . $_GET["name"] . "</h1>";
            // The value of the variable age is found
            echo "<h1>You are " . $_GET["age"] . " years old </h1>";
        ?>
    </body>
</html>
  • In the above code, assume the file name as people.php. Now two query string variables needs to be presented with data when executing this php code in the browser as

http://wikitechy.com/people.php?name=Venkat&age=30