我有基本的登录表格:
....
<input type="text" required autocomplete="off" name="user_name"/>
<input type="password" required autocomplete="off" name="password"/>
....
但是我希望用户从部分选项中选择用户名,而不是从键盘上输入用户名。所以我做了一个选择选项表格
<div class="col-md-2">
<select name="Grade" id="Grade"
data-live-search="true" class="chosen selectpicker form-control" required>
<option value="">Select Grade</option>
</select>
</div>
<div class="col-md-1 col-sm-12" id="lable1"><id="lable1">user</div>
<div class="col-md-2">
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="">Select an Option</option>
</select>
<!--select username-->
</div>
如何处理选择选项表单中的数据,而不是输入用户名
<input type="text" required autocomplete="off" name="user_name"/>
答案 0 :(得分:2)
<form>
元素HTML表单中的所有HTML元素都可以通过PHP的 name 并通过以下变量进行访问:
$_POST['NAME']
或$_GET['NAME']
GET或POST取决于您的表单操作。 这些变量的值就是HTML元素的 value 属性
如form.guide Handling select box (drop-down list) in a PHP form
中所述这是 select 元素
的示例HTML:
<p> What is your Gender? <select name="formGender"> <option value="">Select...</option> <option value="M">Male</option> <option value="F">Female</option> </select> </p>
PHP:
<?php if(isset($_POST['formSubmit']) ) //IF FORM SUBMIT IS PRESSED { $varMovie = $_POST['formMovie']; // Other form inputs $varName = $_POST['formName']; // Other form inputs $varGender = $_POST['formGender']; //Our Select element $errorMessage = ""; // - - - snip - - - } ?>
HTML:
<!--select username-->
<select class="selectpicker form-control" name="user" id="user" autofocus="autofocus" required>
<option value="user1">USER ONE</option>
<option value="user2">USER TWO</option>
</select>
<!--select username-->
您可以在您的PHP中使用:
if(isset($_POST['user']) ){
$name=$_POST['user']; //user1 if USER ONE selected or user2 if second one...
}