What is the PHP form for?
PHP super global variables $_GET and $_POST are used to collect form data (form-data).
PHP - A simple HTML form
The example below shows a simple HTML form that contains two input fields and a submit button:
Example
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
When the user fills out this form and clicks the submit button, the form data will be sent to a PHP file named "welcome.php" for processing. Form data is sent via the HTTP POST method.
To display the submitted data, you can simply echo all variables. The "welcome.php" file looks like this:
<html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>
Output:
Welcome Bill Your email address is Bill.Gates@example.com
The same result can be obtained using the HTTP GET method:
Example
<html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html>
"welcome_get.php" is like this:
<html> <body> Welcome <?php echo $_GET["name"]; ?><br> Your email address is: <?php echo $_GET["email"]; ?> </body> </html>
The above code is very simple. However, the most important part was left out. You need to validate your form data to prevent vulnerabilities in your scripts.
Note: Please pay attention to security when processing PHP forms!
This page does not contain any form validators, it only shows us how to send and receive form data.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of What are PHP forms for?. For more information, please follow other related articles on the PHP Chinese website!