早上好,伙计们,
我有以下PHP函数来创建表单。
echo "<form action='dashboard_ver_3.php' method='post'>
<label> From Date: <input type='date' placeholder='-- select --' name='fromdate'></label>
<label> To Date: <input type='date' placeholder='-- select --' name='todate'></label>
<label> First: <input type='text' name='fName'></label>
<label> Last: <input type='text' name='lName'></label>
<label> Found: <select class='select' name='filterfound'></label>
<option disabled selected value> -- select -- </option>
<option value='GLASSDOOR'>Glassdoor</option>
<option value='INDEED'>Indeed</option>
<option value='CRAIGSLIST'>Craigslist</option>
<option value='MONSTER'>Monster</option>
<option value='FACEBOOK'>Facebook</option>
<option value='FRIEND'>Friend</option>
<option value='FLYER'>Flyer</option>
<option value='GOOGLE'>Google</option>
<option value='RECRUITER'>Recruiter</option>
<option value='NEWSPAPER'>Newspaper</option>
<option value='WALK-IN'>Walk-in</option>
<option value='OTHER'>Other</option>
</select>
<label>Filter by Center: <select class='select' name='filtercenter'></label>
<option disabled selected value> -- select -- </option>
<option value='CAM'>Cambridge, MA</option>
<option value='WOR'>Worcester, MA</option>
<option value='FLL'>Ft. Lauderdale, FL</option>
<option value='COL'>Columbia, MO</option>
<option value='DUR'>Durham, NC</option>
</select>
<input class='submit' type='submit' name='search' value='Search applications ...' />
</form>";
我有以下if语句按姓氏,名字以及姓氏和名字进行搜索。 if语句中的函数都可以正常工作。
if(empty($_POST['filtercenter']) && isset($_POST['fName']) && isset($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>bc<br>";
echo sfName_lName();
}
if(empty($_POST['filtercenter']) && isset($_POST['fName']) && empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>b<br>";
echo sfName();
}
if(empty($_POST['filtercenter']) && empty($_POST['fName']) && isset($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>c<br>";
echo slName();
}
问题是当我按名字,姓氏或两者搜索时;所有三个if语句都运行。如果我再次执行空搜索,则会运行所有这三个if语句。本质上,fName和lName字段始终设置,无论它们是否被填充。
值得一提的是,滤波器中心和滤波器可以保持未填充状态,并且在发布时不会设置它们。我想知道为什么会这样。
答案 0 :(得分:1)
isset()仅检查变量是否存在。你应该检查empty()和!empty()。
这是代码:
if(empty($_POST['filtercenter']) && !empty($_POST['fName']) && !empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>bc<br>";
echo sfName_lName();
}
if(empty($_POST['filtercenter']) && !empty($_POST['fName']) && empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>b<br>";
echo sfName();
}
if(empty($_POST['filtercenter']) && empty($_POST['fName']) && !empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>c<br>";
echo slName();
}
希望这会对你有帮助......