We learned from the previous PHP basic tutorial that the $_GET and $_POST variables are used to retrieve information in the form, such as user input
So what is a form?
#The function of the Web form is to provide an interactive platform for viewers and the website. Forms are mainly used to send data to the server in web pages. For example, your registration information is the form you use. When you fill in the information, you have to submit. Submit is to transfer the content in your form from the client browser. It is transmitted to the server, and after being processed by the PHP program, the information required by the user is passed back to the client browser. By obtaining the user's information, PHP interacts with the Web form.
Note: Forms belong to HTML knowledge and will be explained in detail in our HTML tutorial
Let’s take a look at what the form looks like, shall we?
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网</title> </head> <body> <form action="form.php" method="post"> 名字: <input type="text" name="fname"><br> 年龄: <input type="text" name="age"><br> <input type="submit" value="提交"> </form> </body> </html>
The result of running the above code is as follows:
Yes, this is We are talking about forms, so where do we send the completed form information? When we click submit, the data in our form will be in the form of POST. Sent to the form.php page.
<?php header("Content-type:text/html;charset=utf-8"); //设置编码 echo "欢迎你:".$_POST["fname"] ."<br/>"; echo "你的年龄是:".$_POST['age']; ?>
The running results we sent to form.php:
Welcome: liuqi
Yours Age is: 18
##Form validation
should be in Validate user input whenever possible (via client script). Browser validation is faster and reduces the load on the server. If user input needs to be inserted into the database, you should consider using server validation. A good way to validate a form on the server is to pass the form to itself, rather than jumping to a different page. This way users can get error messages on the same form page. It will be easier for users to find errors.Let’s talk about our form validation in the next section