PHP Post and GET Variables

PHP $_POST and $_GET Variables

More advanced $_GET Variable Example

File: ABOOK/BASIC_SYNTAX/get3.php

This example shows another method for retrieving $_GET variables when a page is posted. Open this page up in a browser and click the link 'Get Two Variables'. Now, the url string contains two variables that can be retrieved with $_GET because the url went from get3.php to get3.php?variable=5&secondvariable=6. Now, click the submit button. When the submit button is clicked, two lines of text are printed out within the condition if($_POST['submit'] == true) and one line is displayed from the condition if($_GET['secondvariable'] == true).  

Note:
Unlike, the previous example, the page posts to a url with the code action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];?>". This is how syntax could be written for posting a page to itself when it has a query string in the url like '?variable=5&secondvariable=6'. Otherwise, you could have written 'action="get3.php?variable=5&secondvariable=6". However, if the $_GET variables are dynamic and can change, a static url does not do much good. Therefore, writing action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];?>" is the best method for self posting to urls with $_GET variables within the url.

<?php 

foreach($_GET as $key => $value){
echo $key."-".$value."<br/>";
}
if($_POST['submit'] == true){
echo "Here is the value of the get variable: ".$_GET['variable']."<br/>";
echo "Here is the value of the post variable named myname: ".$_POST['myname']."<br/>";
}

if($_GET['secondvariable'] == true){
echo "Here is the value of the second get variable: ".$_GET['secondvariable']."<br/>";
}
?>
<br/>
<a href="get3.php?variable=5&secondvariable=6">Get Two Variables</a><br/><br/>

<form method ="post" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];?>" >
<input type="text" name = "myname" value = "Johnny" />
<input type="hidden" name = "req" value = "myrequest" />
<input type = "submit" name ="submit" value = "submit" />
</form>