选择选项时显示未选择选项的表单错误消息
Posted
技术标签:
【中文标题】选择选项时显示未选择选项的表单错误消息【英文标题】:Form showing no option is selected error message when options are selected 【发布时间】:2016-08-17 23:14:01 【问题描述】:我有以下表格:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i)
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i)
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
其工作方式是用户可以从gender
和age
中选择两个选项之一并搜索用户。不需要两个选项都完成搜索。
但是,如果没有选择任何选项,我希望它留在当前页面(`random_chat.php),并回显一条消息。
这是我的方法:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form']))
if (empty ($refined_gender) || empty($age_from) || empty ($age_to))
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
else
// search user
目前,当我将两个搜索选项都留空时,消息会回显,这很好。但是当我搜索男性用户或年龄不为空时,该消息也会回显,这是不应该发生的。
【问题讨论】:
你明白||
和&&
的区别吗?
【参考方案1】:
if 条件不能反映您想要这样修复它的内容
if (empty ($refined_gender) && empty($age_from) && empty ($age_to))
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
else // search user
【讨论】:
??&
??你可能想再看一遍
感谢 RiggsFolly。已编辑!
对不起,这仍然是错误的,请查看@Anant 答案以获得正确的解决方案
@RiggsFolly,你能解释一下吗?你说的是if条件吗?有错吗?【参考方案2】:
您需要将||
更改为&&
,(其他一些更改也在注释中说明):-
<form method="POST" enctype="multipart/form-data"> <!-- remove action ='#' -->
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i)
if(isset($_POST['age_from']) && $_POST['age_from'] == $i) // to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
else
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i)
if(isset($_POST['age_to']) && $_POST['age_to'] == $i)// to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
else
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info" name="submit_form" value="Click to start chat! " />
<form>
<?php
$refined_gender = isset($_POST['gender']) ? $_POST['gender'] : ''; // escape gives me undefined function error at my end so i removed it, but if it is working at your end thhen keep it up as it is.
$age_from = isset($_POST['age_from']) ? $_POST['age_from'] : '';
$age_to = isset($_POST['age_to']) ? $_POST['age_to'] : '';
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) // check always with form posted value not submit button value and also cange || to &&
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
else
// search user
?>
【讨论】:
以上是关于选择选项时显示未选择选项的表单错误消息的主要内容,如果未能解决你的问题,请参考以下文章