PHP Forms and User Input
The $_GET and $_POST variables in PHP are used to retrieve information from forms, such as user input.
PHP Form Handling
It is important to note that when handling HTML forms, PHP can automatically make form elements from the HTML page available for use by PHP scripts.
Example
The example below contains an HTML form with two input boxes and a submit button.
form.html file code:
<
html
<
head
<
meta
charset
=
"
utf-8
"
<
titleRunoob Tutorial(runoob.com)
title
head
<
body
<
form
action
=
"
welcome.php
"
method
=
"
post
"Name:
<
input
type
=
"
text
"
name
=
"
fname
"Age:
<
input
type
=
"
text
"
name
=
"
age
"
<
input
type
=
"
submit
"
value
=
"
Submit
"
form
body
htmlWhen the user fills out the form above and clicks the submit button, the form data is sent to a PHP file named "welcome.php":
welcome.php file code:
Welcome
!
<
br
Your age is
years old.
The demonstration via browser access is as follows:

We will explain the $_GET and $_POST variables in PHP in the next chapter.
PHP Retrieving Dropdown Menu Data
PHP Dropdown Menu Single Select
In the example below, we set up three options for the dropdown menu. The form uses the GET method to obtain data; an empty action attribute value means submitting to the current script. We can retrieve the dropdown menu value via the name attribute of select:
php_form_select.php file code:
http://www.runoob.com ' ; } else if ( $q == ' GOOGLE ' ) { echo ' Google Searchhttp://www.google.com ' ; } else if ( $q == ' TAOBAO ' ) { echo ' Taobao
http://www.taobao.com ' ; } } else { ?>
<
form
action
=
"
"
method
=
"
get
"
<
select
name
=
"
q
"
<
option
value
=
"
"Select a site:
option
<
option
value
=
"
RUNOOB
"
Runoob
option
<
option
value
=
"
GOOGLE
"
Google
option
<
option
value
=
"
TAOBAO
"
Taobao
option
select
<
input
type
=
"
submit
"
value
=
"
Submit
"
formIf the dropdown menu is multi-select (multiple="multiple"), we can set select name="q[]" to obtain it as an array; the following uses the POST method to submit, with the code shown below:
php_form_select_mul.php file code:
' Runoob Tutorial: http://www.runoob.com ' , ' GOOGLE ' => ' Google Search: http://www.google.com ' , ' TAOBAO ' => ' Taobao: http://www.taobao.com ' , ) ; foreach ( $q as $val ) { // PHP_EOL is a constant used for line breaks echo $sites [ $val ] . PHP_EOL ; } } else { ?><
form
action
=
"
"
method
=
"
post
"
<
select
multiple
=
"
multiple
"
name
=
"
q[]
"
<
option
value
=
"
"Select a site:
option
<
option
value
=
"
RUNOOB
"
Runoob
option
<
option
value
=
"
GOOGLE
"
Google
option
<
option
value
=
"
TAOBAO
"
Taobao
option
select
<
input
type
=
"
submit
"
value
=
"
Submit
"
formIn a PHP radio button form, the value of the name attribute is consistent and the value attributes are different; the code is shown below:
php_form_radio.php file code:
http://www.runoob.com ' ; } else if ( $q == ' GOOGLE ' ) { echo ' Google Searchhttp://www.google.com ' ; } else if ( $q == ' TAOBAO ' ) { echo ' Taobao
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
=
"
Submit
"
formA PHP checkbox can select multiple values:
php_form_select_checkbox.php file code:
' Runoob Tutorial: http://www.runoob.com ' , ' GOOGLE ' => ' Google Search: http://www.google.com ' , ' TAOBAO ' => ' Taobao: http://www.taobao.com ' , ) ; foreach ( $q as $val ) { // PHP_EOL is a constant used for line breaks echo $sites [ $val ] . PHP_EOL ; } } else { ?><
form
action
=
"
"
method
=
"
post
"
<
input
type
=
"
checkbox
"
name
=
"
q[]
"
value
=
"
RUNOOB
"
Runoob
<
br
<
input
type
=
"
checkbox
"
name
=
"
q[]
"
value
=
"
GOOGLE
"
Google
<
br
<
input
type
=
"
checkbox
"
name
=
"
q[]
"
value
=
"
TAOBAO
"
Taobao
<
br
<
input
type
=
"
submit
"
value
=
"
Submit
"
formWe should validate user input as much as possible (through client-side scripts). Browser validation is faster and can reduce server load.
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 side is to pass the form data to the current page (asynchronous submission is better), rather than jumping to a different page. This way, the user can get error messages on the same form page, making it easier for them to spot errors.