PHP forms and user input

The $_GET and $_POST variables in PHP are used to retrieve information in a form, such as user input.

Before studying this chapter, you can review the HTML form first

A form is an area containing form elements.

Form elements allow users to enter content in the form, such as text areas, drop-down lists, radio-buttons, checkboxes, etc.

The form uses the form tag <form> to set the

Specific review URL: HTML form

PHP form processing

There is one very important thing worth mentioning Note that when processing HTML forms, PHP can automatically make form elements from the HTML page available to PHP scripts.

Example

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

form.html file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<form action="welcome.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>

</body>
</html>

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

welcome.php file is as follows:

                                                                                                              Your age is <?php echo $_POST["age"]; ?> years.

The demonstration of accessing through the browser is as follows:

101.gif


#We will explain $_GET in PHP in the following chapters and the $_POST variable.

Form Validation

User input should be validated (via client script) whenever possible. 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.

Continuing Learning
||
<html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <form action="welcome.php" method="post"> 名字: <input type="text" name="fname"> 年龄: <input type="text" name="age"> <input type="submit" value="提交"> </form> </body> </html>
submitReset Code