* *
\\n\\nThe $_GET and $_POST variables in PHP are used to retrieve information from forms, such as user input.
\\n\\n* *
\\n\\nPHP Form Processing
\\n\\nThere is an important thing to note: when processing HTML forms, PHP can automatically convert form elements from HTML pages into variables available for PHP scripts.
\\n\\nExample
\\n\\nThe following example contains an HTML form with two input fields and a submit button.
\\n\\nCode for form.html file:
\\n\\n Name: Age: \\n\\nWhen users fill out the above form and click the submit button, the form data will be sent to a PHP file named "welcome.php":
\\n\\nCode for welcome.php file:
\\n\\nWelcome!Your Age is years old.\\n\\n
Access the demonstration through the browser as follows:
\\n\\nWe will explain the $_GET and $_POST variables in PHP in the next chapter.
\\n\\n* *
\\n\\nPHP Getting Data from Dropdown Menus
\\n\\nPHP Single Selection Dropdown Menu
\\n\\nIn the following example, we set three options for the dropdown menu. The form uses GET method to get data. An empty action attribute value means submitting to the current script. We can get the value of the dropdown menu through the name attribute of the select element:
\\n\\nCode for php_form_select.php file:
\\n\\n<?php$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : ''; if($q){if($q ==''){echo''; }else if($q =='GOOGLE'){echo'Google Search
http://www.google.com'; }else if($q =='TAOBAO'){echo'Taobao
http://www.taobao.com'; }}else{?>\\n\\nSelect a site:TutorialGoogleTaobao\\n\\n
PHP Multiple Selection Dropdown Menu
\\n\\nIf the dropdown menu allows multiple selections (multiple="multiple"), we can get values as an array by setting the select's name="q[]". The following code uses POST method to submit, as shown below:
\\n\\nCode for php_form_select_mul.php file:
\\n\\n': ', 'GOOGLE' =>'Google Search: http://www.google.com', 'TAOBAO' =>'Taobao: http://www.taobao.com', ); foreach($q as$val){echo$sites[$val] . PHP_EOL; }}else{?>\\n\\nSelect a site:TutorialGoogleTaobao\\n\\n* *
\\n\\nRadio Button Form
\\n\\nIn PHP radio button forms, the name attribute values are consistent while the value attributes are different, as shown in the code below:
\\n\\nCode for php_form_radio.php file:
\\n\\n<?php$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : ''; if($q){if($q ==''){echo''; }else if($q =='GOOGLE'){echo'Google Search
http://www.google.com'; }else if($q =='TAOBAO'){echo'Taobao
http://www.taobao.com'; }}else{\\n\\n?> Google Taobao \\n\\n
* *
\\n\\nCheckbox
\\n\\nPHP checkboxes can select multiple values:
\\n\\nCode for php_form_select_checkbox.php file:
\\n\\n': ', 'GOOGLE' =>'Google Search: http://www.google.com', 'TAOBAO' =>'Taobao: http://www.taobao.com', ); foreach($q as$val){echo$sites[$val] . PHP_EOL; }}else{\\n\\n?>Taobao
\\n\\n
* *
\\n\\nForm Validation
\\n\\nWe should validate user input as much as possible (through client-side scripts). Browser validation is faster and can reduce server load.
\\n\\nIf user input needs to be inserted into a database, you should consider using server-side validation. A good way to validate forms on the server is to send the form data to the current page (asynchronous submission is better) rather than redirecting to a different page. This way users can get error messages on the same form page. Users can also more easily discover errors.
YouTip