Easy Tutorial
❮ Php Mysql Create Table Func Misc Highlight String ❯

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 processing HTML forms, PHP can automatically convert form elements from HTML pages into usable PHP scripts.

Example

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

form.html File Code:

<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</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>
</html>

When the user fills out the form above and clicks the submit button, the form data will be sent to the PHP file named "welcome.php":

welcome.php File Code:

Welcome <?php echo $_POST["fname"]; ?>!<br>
Your age is <?php echo $_POST["age"]; ?> years old.

The demonstration in the browser 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 Selection

In the following example, we set up a dropdown menu with three options. The form uses the GET method to retrieve data, and the action attribute is empty, indicating submission to the current script. We can obtain the value of the dropdown menu through the name attribute of the select element:

php_form_select.php File Code:

<?php
$q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
if($q) {
        if($q =='tutorialpro') {
                echo 'tutorialpro.org<br>http://www.tutorialpro.org';
        } else if($q =='GOOGLE') {
                echo 'Google Search<br>http://www.google.com';
        } else if($q =='TAOBAO') {
                echo 'Taobao<br>http://www.taobao.com';
        }
} else {
?><form action="" method="get"> 
    <select name="q">
    <option value="">Select a site:</option>
    <option value="tutorialpro">tutorialpro</option>
    <option value="GOOGLE">Google</option>
    <option value="TAOBAO">Taobao</option>
    </select>
    <input type="submit" value="Submit">
    </form><?php
}
?>

PHP Dropdown Menu Multiple Selection

If the dropdown menu is multi-select (multiple="multiple"), we can retrieve it as an array by setting select name="q[]". The following example uses the POST method:

php_form_select_mul.php File Code:

<?php
$q = isset($_POST['q'])? $_POST['q'] : '';
if(is_array($q)) {
    $sites = array(
            'tutorialpro' => 'tutorialpro.org: http://www.tutorialpro.org',
            'GOOGLE' => 'Google Search: http://www.google.com',
            'TAOBAO' => 'Taobao: http://www.taobao.com',
    );
    foreach($q as $val) {
        // PHP_EOL is a constant for newline
        echo $sites[$val] . PHP_EOL;
    }
}
?>
} else {
?><form action="" method="post"> 
    <select multiple="multiple" name="q[]">
    <option value="">Select a site:</option>
    <option value="tutorialpro">tutorialpro</option>
    <option value="GOOGLE">Google</option>
    <option value="TAOBAO">Taobao</option>
    </select>
    <input type="submit" value="Submit">
    </form><?php
}
?>
❮ Php Mysql Create Table Func Misc Highlight String ❯