Home > Article > Backend Development > PHP implements getting the value of text box, password field and button
This article mainly introduces the PHP example code for obtaining the value of the text box, password field, and button. Friends who need it can refer to it
Getting the value submitted by the form element is the most basic operation method in the form application . This section defines the POST()
method to submit data, and provides a detailed explanation of obtaining the value submitted by the form element.
Get the value of the text box, password field, button
Get the form data, actually get the different form elements data. The name in the ff9c23ada1bcecdd1a0fb5d5a0f18437 tag is an attribute that all form elements have, that is, the name of this form element. When using it, you need to use the name attribute to get the value attribute value of the response. Therefore, all controls added must define corresponding name attribute values. In addition, try not to repeat the naming of controls to avoid errors in the data obtained.
During the program development process, the methods for obtaining the values of text boxes, password fields, hidden fields, buttons and text fields are the same. They all use the name attribute to obtain the corresponding value attribute value. This section only takes obtaining the data information in the text box as an example to explain the method of obtaining form data. I hope friends can draw inferences from one example and try to obtain other control values by themselves.
The following uses a login example to learn how to obtain the information of the text box. In the example below, if the user clicks the "Login" button, the username and password are obtained.
The specific implementation steps are as follows:
(1) Use any development tool to create a PHP dynamic page and name it index.php.
(2) Add a form, a text box and a submit button, the code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>form</title> </head> <body> <form action="" method="post" name="form1"> <table width="500" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="500" height="30"> 用户名:<input type="text" name="user" size="12"> 密 码:<input type="password" name="pwd" id="pwd" size="12"> <input type="submit" name="submit" value="登录"> </td> </tr> </table> </form> </body> </html>
(3) In< ;form>Add a PHP tag anywhere outside the form element, and use the if conditional statement to determine whether the user has submitted the form. If the condition is true, use the echo statement to output the user name and password obtained using the $_POST[] method. The code is as follows Reality:
<?php if( $_POST["submit"] == "登录"){ // 判断提交的按钮名称是否为“登录” // 使用 echo 语句输出使用 $_POST[] 方法获取的用户名和密码 echo "用户名为:". $_POST['user'] . "<br >密码为:" . $_POST['pwd']; } ?>
Note: When applying a text box to pass a value, be sure to set the name attribute of the text box correctly, and there should be no spaces; when getting When obtaining the value of a text box, the text box name used must be the same as the name set in the form text box, otherwise the value of the text box will not be obtained.
(4) Enter the running address in the browser and press the Enter key to get the running result as shown below:
The above is the summary of this article All content, I hope it will be helpful to everyone's study.
Related recommendations:
Two implementation methods of php file upload_php skills
PHPArray function array_multisort() usage example analysis_php skills
##PHPFunction import_request_variables() usage analysis_ php skills
The above is the detailed content of PHP implements getting the value of text box, password field and button. For more information, please follow other related articles on the PHP Chinese website!