dongling5411 2018-01-03 16:58
浏览 53
已采纳

输入字段已设置,即使它们未填充也是如此

Good morning folks,

I have the following PHP function to create a form.

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>";

I have the following if statements to search by last name, first name, and both last and firstname. The functions within the if statements all work perfectly fine.

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();

}

The issue is when I search by first name, last name, or both; all three if statements run. If I do an empty search, again, all three of these if statements run. Essentially, the fName and lName fields are always set, regardless of them being filled or not.

It is worth mentioning that the filtercenter and filterfound can be left unfilled and they are not set when posted. I am wondering why this is the case.

  • 写回答

1条回答 默认 最新

  • douwo5710 2018-01-03 17:23
    关注

    isset() is only check the variable is exists or not. You should check with empty() and !empty().

    here is the code:

    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();
    
    }
    

    Hope this will help you...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体