[英]Make dropdown selected using $_POST value
I have a dropdown menu like this 我有一个这样的下拉菜单
<form method="post">
Filter by state:
<select name="state" id="state">
<option value=""></option>
<option value="AL">Alabama</option><option value="AK">Alaska</option>
<option value="AZ">Arizona</option><option value="AR">Arkansas</option>
<option value="CA">California</option><option value="CO">Colorado</option>
<option value="WY">Wyoming</option><option value="Other">Other</option>
<input type="submit" id="statefilt" name="statefilt" onclick="" value="Submit" />
</select>
</form>
When the page is reloaded I get the $_POST to be 重新加载页面后,我得到了$ _POST
Array ([state]=>AL [statefilt]=>Submit)
Is there a way to use the $_POST['state']
and make that the selected option if the value is not empty? 如果值不为空,是否可以使用$_POST['state']
并使其成为选定的选项?
You can use selected
attribute when the $_POST
value is same as <option>
value. $_POST
值与<option>
值相同时,可以使用selected
属性。
And, <input type="submit" id="statefilt" name="statefilt" onclick="" value="Submit" />
is wrong inside <select></select>
并且, <input type="submit" id="statefilt" name="statefilt" onclick="" value="Submit" />
<select></select>
内的<input type="submit" id="statefilt" name="statefilt" onclick="" value="Submit" />
是错误的
<form method="post">
Filter by state:
<select name="state" id="state">
<option value="" <?if($_POST['state'] == ""){echo "selected";}?>></option>
<option value="AL" <?if($_POST['state'] == "AL"){echo "selected";}?>>Alabama</option>
<option value="AK" <?if($_POST['state'] == "AK"){echo "selected";}?>>Alaska</option>
<option value="AZ" <?if($_POST['state'] == "AZ"){echo "selected";}?>>Arizona</option>
<option value="AR" <?if($_POST['state'] == "AR"){echo "selected";}?>>Arkansas</option>
</select>
<input type="submit" id="statefilt" name="statefilt" onclick="" value="Submit" />
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.