PHP form
The $_GET and $_POST variables in PHP are used to retrieve information from a form, such as user input.
PHP form processing
One very important thing to note is that when processing HTML forms, PHP can automatically process the form elements from the HTML page Become available to PHP scripts.
Example
The following example contains an HTML form with two input boxes and a submit button.
form.html The file code is as follows:
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="welcome.php" method="post"> 名字: <input type="text" name="fname"> 年龄: <input type="text" name="age"> <input type="submit" value="提交"> </form> </body> </html>When the user fills out the above form and clicks the submit button, the form data will be sent to the name The PHP file for "welcome.php":
welcome.php file is as follows:
欢迎 <?php echo $_POST["fname"]; ?>!<br> 你的年龄是 <?php echo $_POST["age"]; ?> 岁。
The demo accessed through the browser is as follows:
We will explain the $_GET and $_POST variables in PHP in the next chapter.
PHP Get the data of the drop-down menu
PHP Drop-down menu radio selection
In the following example, we set up 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 obtain the drop-down menu through the name attribute of select. Menu value:
php_form_select.php File code:
<?php$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';if($q) { if($q =='PHP') { echo 'php中文网教程<br>http://www.php.cn'; } else if($q =='GOOGLE') { echo 'Google 搜索<br>http://www.google.com'; } else if($q =='TAOBAO') { echo '淘宝<br>http://www.taobao.com'; }} else { ?> <form action="" method="get"> <select name="q"> <option value="">选择一个站点:</option> <option value="PHP">PHP</option> <option value="GOOGLE">Google</option> <option value="TAOBAO">Taobao</option> </select> <input type="submit" value="提交"> </form> <?php}?>
PHP drop-down menu multi-select
If the drop-down menu is For multiple selections (multiple="multiple"), we can obtain it by setting select name="q[]" as an array, and submit it using POST. The code is as follows:
php_form_select_mul.php File code:
<?php$q = isset($_POST['q'])? $_POST['q'] : '';if(is_array($q)) { $sites = array( 'PHP' => 'php中文网: http://www.php.cn', 'GOOGLE' => 'Google 搜索: http://www.google.com', 'TAOBAO' => '淘宝: http://www.taobao.com', ); foreach($q as $val) { // PHP_EOL 为常量,用于换行 echo $sites[$val] . PHP_EOL; } } else { ?> <form action="" method="post"> <select multiple="multiple" name="q[]"> <option value="">选择一个站点:</option> <option value="PHP">PHP</option> <option value="GOOGLE">Google</option> <option value="TAOBAO">Taobao</option> </select> <input type="submit" value="提交"> </form> <?php}?>
Radio button form
PHP The value of the name attribute in the radio button form is consistent, but the value value is different , the code is as follows:
php_form_radio.php file code:
<?php$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';if($q) { if($q =='PHP') { echo 'php中文网教程<br>http://www.php.cn'; } else if($q =='GOOGLE') { echo 'Google 搜索<br>http://www.google.com'; } else if($q =='TAOBAO') { echo '淘宝<br>http://www.taobao.com'; }} else { ?><form action="" method="get"> <input type="radio" name="q" value="RUNOOB" />Runoob <input type="radio" name="q" value="GOOGLE" />Google <input type="radio" name="q" value="TAOBAO" />Taobao <input type="submit" value="提交"></form> <?php}?>
checkbox checkbox
PHP checkbox The check box can select multiple values:
php_form_select_checkbox.php File code:
<?php$q = isset($_POST['q'])? $_POST['q'] : '';if(is_array($q)) { $sites = array( 'PHP' => 'php中文网教程: http://www.php.cn', 'GOOGLE' => 'Google 搜索: http://www.google.com', 'TAOBAO' => '淘宝: http://www.taobao.com', ); foreach($q as $val) { // PHP_EOL 为常量,用于换行 echo $sites[$val] . PHP_EOL; } } else { ?> <form action="" method="post"> <input type="checkbox" name="q[]" value="PHP"> PHP<br> <input type="checkbox" name="q[]" value="GOOGLE"> Google<br> <input type="checkbox" name="q[]" value="TAOBAO"> Taobao<br> <input type="submit" value="提交"> </form> <?php}?>
Form validation
We should be as accurate as possible for the user Input is validated (via client script). Browser validation is faster and takes less pressure off the server.
If user input needs to be inserted into the 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.
Recommended related articles: "Detailed introduction to PHP obtaining form form data"
Recommended practical tutorials: "PHP Forms and User Input》