Get HTML form of HTML Form data with PHP's super variable $_POST

  • 2020-05-05 11:01:57
  • OfStack

When the method of the HTML form (HTML Form) is get, $_GET is used to get the HTML Form data.

When method of the HTML form (HTML Form) is post, $_POST is used to get the HTML Form data.

For the difference between get and post in HTML Form, see get and post in HTML Form.

Get the HTML form (HTML Form) text field (input type="text") data
Below is an HTML file. This HTML contains an HTML Form, which is mainly used to ask the user to enter the user name.
 
<html> 
<body> 
<form action ="post.php" method ="post"> 
Name: <input type="text" name="username" /> 
<input type ="submit" value="ok" /> 
</form> 
</body> 
</html> 

When you type your name in the HTML Form textbox, such as "Jacky", and then click the ok button, it will jump to post.php, and the output will be You are Jacky. The source code for post.php is as follows:
 
<html> 
<body> 
You are <?php echo $_POST["username"]?>. 
</body> 
</html> 

Take the name value of a form control to get the data for that form control.

For example, "username" is the name value of the form control text input field,

< input type="text" name="username" / >
The data for this text input box can be obtained with $_POST["username"].

< ?php echo $_POST["username"]? >
Get the HTML form (HTML Form) menu (input type="radio") data
Take the name value of the form menu to get the value of the form menu.

Below is an HTML file with a form marquee. The code is
 
<html> 
<body> 
<form action = "radiopost.php" method = "post"> 
<input type="radio" name="fruit" value = "Apple">Apple</input><br /> 
<input type="radio" name="fruit" value = "Orange">Orange</input><br /> 
<input type="radio" name="fruit" value = "Mango">Mango</input><br /> 
<input type="submit" value="ok"> 
</form> 
</body> 
</html> 

In the HTML file, select any item, such as "Orange", and then click the button ok, the browser will jump to radiopost.php, radiopost.php output is Orange. The source code for radiopost.php is as follows:
 
<html> 
<body> 
<?php echo $_POST["fruit"]?> 
</body> 
</html> 

fruit in $_POST["fruit"] is the name value of the form's marquee.

Get the HTML form (HTML Form) checkbox (input type="checkbox") data
The user can select multiple values from the HTML Form check box, so $_POST gets more than one value, an array.

When writing the name value of the HTML Form check box, note that the name value is followed by [].

As an example, name="fruit[]" :
 
<html> 
<body> 
<form action = "checkboxpost.php" method = "post"> 
<input type="checkbox" name="fruit[ ]" value = "Apple">Apple</input><br /> 
<input type="checkbox" name="fruit[ ]" value = "Orange">Orange</input><br /> 
<input type="checkbox" name="fruit[ ]" value = "Mango">Mango</input><br /> 
<input type="submit" value="ok"> 
</form> 
</body> 
</html> 

The source code for checkboxpost.php is as follows:
 
<html> 
<body> 
<?php 
echo count($_POST["fruit"]),"<br />"; 
foreach ($_POST["fruit"] as $value) 
{echo $value,"<br />"; 
} 
?> 
</body> 
</html> 

If you select Orange and Mango, and click the OK button, the browser will jump to checkboxpost.php, the number of elements of the array $_POST["fruit"] will be obtained by using the count function.

Related articles: