duanemei2194
duanemei2194
2017-08-21 10:25
浏览 20
已采纳

我从select到POST获取数据,即使使用了isset,它也无法正常使用条件语句?

I am fetching data from HTML written in my page and below this, I have my PHP code connected to an Oracle database on the same page. I am unable to check and compare the value rightly.

It's giving always false, I don't know why. I am able to resolve this using my chooseQuery variable as truthy/false but I want to know the reason of not working this condition. I think it may be only my server/machine related problem but I want to know the other reason if exists.

HTML Code:

<form action="" method="POST">
  <label for="chooseQuery">Choose Query :</label>
  <select name="chooseQuery" id="chooseQuery">
    <option value="" selected="selected">Choose one of the following to get data</option>
    <option value="Direct Approval Mod ID wise">Direct Approval Mod ID wise</option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
    <option value=""></option>
  </select>
  <label for="start_date">Start Date :</label>
  <input type="date" id="start_date" name="start_date" value="<?php echo date('Y-m-d',strtotime('-1 week')); ?>">
  <label for="end_date">End Date :</label>
  <input type="date" id="end_date" name="end_date" value="<?php echo date('Y-m-d');?>">
  <br>
  <input type="submit" value="Submit" name="submit">
</form>

PHP Code :

    if($con = $config->connectDB('imblrtR')){
    if(isset($_POST['submit'])){
      $chooseQuery = isset($_REQUEST['chooseQuery']) ? $_REQUEST['chooseQuery'] : '';
      $start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : '';
      $end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : '';
      echo "
".$chooseQuery;
      if($chooseQuery!=0 && $chooseQuery!='' && $chooseQuery!='0')
      {
        echo "ok";
        //getData($chooseQuery,$start_date,$end_date);
      }
      else{
        echo "Choose Right Option";
      }
      oci_close($con);
    }
  }
  else
  {
    echo "not connected

";
  }

This is always printing Choose Right Option even after showing $chooseQuery value as Direct Approval Mod ID wise.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzi7711
    douzi7711 2017-08-21 10:37
    已采纳

    Your $chooseQuery!=0 condition was not being met because when comparing a string with integer, PHP tries to convert that string to an integer. if it can't, then that string is evaluated as 0. so your condition $chooseQuery!=0 basically became 0!=0 which failed everytime.

    Change your conditional statement to below.

    if(!empty($chooseQuery))
      {
        echo "ok";
        //getData($chooseQuery,$start_date,$end_date);
      }
      else{
        echo "Choose Right Option";
      }**strong text**
    
    点赞 评论
  • dougui4325
    dougui4325 2017-08-21 10:32

    Try replacing

    if($chooseQuery!=0 && $chooseQuery!='' && $chooseQuery!='0')
    

    to

    if(!empty($chooseQuery))
    

    It should work if you are getting any value in $chooseQuery variable!

    点赞 评论

相关推荐