Home  >  Article  >  Backend Development  >  PHP gets the value of form text box, password field and button

PHP gets the value of form text box, password field and button

伊谢尔伦
伊谢尔伦Original
2017-04-19 09:51:095108browse

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 values ​​submitted by form elements.

Get the values ​​of text boxes, password fields, and buttons

Getting form data is actually getting the data of different form elements. The name in the

tag is the attribute that all form elements have, which 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 the corresponding name attribute value. In addition, the names of controls should not be repeated as much as possible to avoid errors in the data obtained.

During the program development process, the method of obtaining the value of the text box, password field, hidden field, button and text field is 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.

Let’s use the login example to learn how to get 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) Add PHP tag anywhere outside the

form element symbol, use the if conditional statement to determine whether the user has submitted the form. If the judgment condition is true, use the echo statement to output the user obtained using the $_POST[] method. name and password, the code is as follows:
<?php
if( $_POST["submit"] == "登录"){             // 判断提交的按钮名称是否为“登录”
 // 使用 echo 语句输出使用 $_POST[] 方法获取的用户名和密码
 echo "用户名为:". $_POST[&#39;user&#39;] . "<br >密码为:" . $_POST[&#39;pwd&#39;];
}
?>

Note: When using a text box to pass value, you must correctly set the name attribute of the text box, and there should be no spaces; when getting the value of the 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:

PHP gets the value of form text box, password field and button

The above is the detailed content of PHP gets the value of form text box, password field and button. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn