YouTip LogoYouTip

Php Forms

* *

\\n\\n

The $_GET and $_POST variables in PHP are used to retrieve information from forms, such as user input.

\\n\\n

* *

\\n\\n

PHP Form Processing

\\n\\n

There 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\\n

Example

\\n\\n

The following example contains an HTML form with two input fields and a submit button.

\\n\\n

Code for form.html file:

\\n\\n Name: Age: \\n\\n

When 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\\n

Code for welcome.php file:

\\n\\nWelcome!
Your Age is years old.\\n\\n

Access the demonstration through the browser as follows:

\\n\\n

Image 1

\\n\\n

We will explain the $_GET and $_POST variables in PHP in the next chapter.

\\n\\n

* *

\\n\\n

PHP Getting Data from Dropdown Menus

\\n\\n

PHP Single Selection Dropdown Menu

\\n\\n

In 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\\n

Code 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\\n

If 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\\n

Code 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\\n

Radio Button Form

\\n\\n

In PHP radio button forms, the name attribute values are consistent while the value attributes are different, as shown in the code below:

\\n\\n

Code 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\\n

Checkbox

\\n\\n

PHP checkboxes can select multiple values:

\\n\\n

Code 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?>
Google
Taobao
\\n\\n

* *

\\n\\n

Form Validation

\\n\\n

We should validate user input as much as possible (through client-side scripts). Browser validation is faster and can reduce server load.

\\n\\n

If 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.

← Php GetPhp Functions β†’