The $_GET and $_POST variables in PHP are used to retrieve information from a form, such as user input.
One important thing to note is that when processing HTML forms, PHP can automatically make form elements from the HTML page available to PHP scripts.
The example below contains an HTML form with two input fields and a submit button.
<html><head><metacharset="utf-8"><title>Source Code Network (downcodes.com)</title></head><body> <formaction="welcome.php" method="post"> Name: <inputtype="text"name="fname">Age: <inputtype="text"name="age"><inputtype="submit"value="submit"></form> </body></ html>
When the user fills out the above form and clicks the submit button, the form data will be sent to the PHP file named "welcome.php":
Welcome<?phpecho$_POST["fname"]; ?>!<br>Your age is <?phpecho$_POST["age"]; ?> years old.
We will explain the $_GET and $_POST variables in PHP in the next chapter.
In the following example, we set three options for the drop-down menu. The form uses the GET method to obtain data. If the action attribute value is empty, it means it is submitted to the current script. We can get the value of the drop-down menu through the name attribute of select:
<?php$q= isset($_GET['q'])? htmlspecialchars($_GET['q']): '';if($q){ if($q=='CODERCTO'){ echo' Source code network<br>http://www.downcodes.com'; }elseif($q=='GOOGLE'){ echo'Google search<br>http://www.google.com'; }elseif($q=='TAOBAO'){ echo'Taobao<br>http://www.taobao.com'; }}else{?><formaction=""method="get"> <selectname=" q"> <optionvalue="">Select a site:</option> <optionvalue="CODERCTO">codercto</option> <optionvalue="GOOGLE">Google</option> <optionvalue="TAOBAO">Taobao</option> </select> <inputtype="submit"value="submit"> </form><?php}?>
If the drop-down menu is multi-select (multiple="multiple"), we can obtain it by setting select name="q[]" as an array. The following is submitted using POST. The code is as follows:
<?php$q= isset($_POST['q'])? $_POST['q']: '';if(is_array($q)){ $sites= array( 'CODERCTO'=> 'Source Code Network : http://www.downcodes.com', 'GOOGLE'=> 'Google search: http://www.google.com', 'TAOBAO'=> 'Taobao: http://www.taobao.com', ); foreach($qas$val){ //PHP_EOL is a constant, used for line breaks echo$sites[$val]. PHP_EOL; } }else{?><formaction=" "method="post"> <selectmultiple="multiple"name="q[]"> <optionvalue="">Select a site:</option> <optionvalue="CODERCTO">codercto</option> <optionvalue="GOOGLE">Google</option> <optionvalue="TAOBAO">Taobao</option> </select> <inputtype="submit"value="Submit"> </form><?php}?>
The values of the name attribute in the PHP radio button form are consistent, but the value values are different. The code is as follows:
<?php$q= isset($_GET['q'])? htmlspecialchars($_GET['q']): '';if($q){ if($q=='CODERCTO'){ echo' Source code network<br>http://www.downcodes.com'; }elseif($q=='GOOGLE'){ echo'Google search<br>http://www.google.com'; }elseif($q=='TAOBAO'){ echo'Taobao<br>http://www.taobao.com'; }}else{?><formaction=""method="get"> <inputtype=" radio"name="q"value="CODERCTO"/>codercto <inputtype="radio"name="q"value="GOOGLE"/>Google <inputtype="radio"name="q"value="TAOBAO"/>Taobao <inputtype="submit"value="submit"></form><?php}?>
PHP checkbox checkbox can select multiple values:
<?php$q= isset($_POST['q'])? $_POST['q']: '';if(is_array($q)){ $sites= array( 'CODERCTO'=> 'Source Code Network : http://www.downcodes.com', 'GOOGLE'=> 'Google search: http://www.google.com', 'TAOBAO'=> 'Taobao: http://www.taobao.com', ); foreach($qas$val){ //PHP_EOL is a constant, used for line breaks echo$sites[$val]. PHP_EOL; } }else{?><formaction=" "method="post"> <inputtype="checkbox"name="q[]"value="CODERCTO">codercto<br> <inputtype="checkbox"name="q[]"value="GOOGLE">Google<br> <inputtype="checkbox"name="q[]"value="TAOBAO">Taobao<br> <inputtype=" submit"value="submit"></form><?php}?>
We should validate user input (via client-side script) whenever possible. Browser validation is faster and takes less pressure off the server.
If user input needs to be inserted into a database, you should consider using server validation. A good way to validate a form on the server is to pass the form data to the current page (asynchronous submission is better), rather than jumping to a different page. This way users can get error messages on the same form page. It will be easier for users to find errors.