PHP forms and u...LOGIN

PHP forms and user input

PHP Forms and User Input

Before introducing the form, let’s first understand the basic knowledge about the form:

1. Form Tag<form>

<form> tag is used to create HTML forms for user input.

Forms can contain input elements, such as text fields, check boxes, radio buttons, submit buttons, etc.

The form can also contain menus, textarea, fieldset, legend and label elements.

The form is used to transmit data to the server.

<form>Tag

Required attributes:

Action, specifies where to send form data when the form is submitted.

Optional attributes:

Method, specifies how to send form data.

Name specifies the name of the form.

Enctype, specifies how to encode the data before sending it to the server.

2. Text box, user input

Definition: <input type=”text” name=”wenbenkuang” >

Server usage: $_POST['wenbenkuang'];

Note: The text box is the most common form element and corresponds to a name The form of a value.

3. Password box provides an area for users to enter passwords.

Definition: <input type=”password” name=”mimakuang”>

Server usage: $_POST['mimakuang'];

Note: The password box uses cipher text to represent the plain text in the text box to increase data security, but it cannot check whether the input is correct, so Class One will provide two password boxes to verify that the password entered by the user is consistent.

4. Hidden fields provide invisible form elements.

Definition: <input type=”hidden” name=”yincangyu” value=”123”>

Server usage: $_POST['yincangyu'] ;

Note: Hidden fields are form elements that cannot be seen on the page. We usually use this method to pass the value of the id class.

5. Check boxes provide multiple selection operations.

Definition:<input type=”checkbox” name=”fuxuankuang[]” value=”1” checked=”checked”>

<input type="checkbox" name="fuxuankuang[]" value="2">

Server usage: $_POST['fuxuankuang']; The value is an array.

Note: We place a group of check boxes by setting the name attribute to the same array. The checked attribute specifies whether the check box's initial state is checked.

6. The radio button provides the operation of selecting one among multiple options.

Definition:<input type=”radio” name=”danxuankuang” value=”1” checked=”checked”>

<input type= "radio" name="danxuankuang" value="2">

Server usage: $_POST['danxuankuang']; The value is a number, not an array.

Note: We place a group of radio button boxes in the same way by setting the name attribute to the same way. The checked attribute specifies whether the initial state of the radio button is checked.

7. Text field provides a large text input area.

Definition: <textarea name=”wenbenyu”>1234</textarea>

Server usage: $_POST['wenbenyu”];

Note: The text field is a double label. We can control its width and height through the cols and rows attributes of the text field. When we need to enter a large paragraph of text. A text field is required.

8. List provides a limited list for selection.

Definition:

< ;select name="liebiao">

<option value="1">Option 1</option>

<option value="2">Option 2< /option>

<option value="3" selected="selected" >Option 3</option>

<option value="4">Option 4</ option>

</select>

Server usage: $_POST["liebiao"]; Whichever you select, its value will be the value attribute of that option The value of The value attribute of each option value should be written to the option tag. We can control whether multiple selections can be made through the multiple attribute of the select tag:

<select multiple=”multiple”>

You can also control the number of displayed entries through the size attribute

9. File field, used to provide upload file elements

Definition: <input type=”file. ” name=”wenianyu”>

Server usage: $_FILES['wenjianyu']; what you get is an array with five elements, including some basic information about the uploaded file.

Note: If you want to use a form to upload files, you must add the following attribute to the form tag: <form enctype="multiplart/form-data", and then we obtain it through the global array variable $_FILES Upload file information.

10. Reset, the reset button is used to restore the initial state of the form

Definition: <input type=”reset” value =”Reset”>

Note: The reset button is a very commonly used function to restore the initial value of the form. The value attribute specifies the text on the button.

11. Submit, submit form button.

Definition: <input type=”submit” name=”submit” value=”submit”>

Note: When When this button is clicked, the form is submitted to the specified page. This button can have a name attribute value. Generally, we provide the value of $_POST[‘submit’] on the server side to determine whether the current request comes from a form submission.

The above content introduces the content, functions and precautions contained in the form. Now we will learn the specific content of the form in detail.

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

PHP Form Processing

One very important thing to note is that when processing HTML forms, PHP can automatically turn form elements from the HTML page into For use by 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:

欢迎 <?php echo $_POST["fname"]; ?>!<br>
你的年龄是 <?php echo $_POST["age"]; ?>  岁。

The demo accessed through the browser is as follows:

QQ图片20161009111553.png


We will explain the $_GET and $_POST variables in PHP in the next chapter.


Next Section
<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
ChapterCourseware