douchuilai2355 2014-04-28 04:04
浏览 55
已采纳

如何检查下拉框是否与if语句匹配[PHP]

I have been coding a script, and I am stuck at one small part. I am checking to make sure that the form the user submits is secured. But I'm not sure how to approach the dropdown box, this is the code I have so far:

if ($_POST['type'] != '1' || $_POST['type'] != '2' || $_POST['type'] != '3') // Checks to see if the submitted type matches, prevents exploiting.
{
    $supportErrors[] = "Support type you've chosen was not found, possibly trying to exploit this script?";
}

I am not sure if this is the right way to check its value. This is my HTML side for this part of the script:

<select name="type" id="type" size="1" style="border: 1px solid #000000; background-color: #FFFFFF;">
      <option value="1" selected>Support</option>
      <option value="2">Question</option>
      <option value="3">Complaint</option>
</select>

Now, to my understanding, the value is the data being posted VIA 'type' - so, in my mind, shouldn't it post that value (1 or 2 or 3) and then in the PHP side, it checks if it isn't 1, 2 or 3?

  • 写回答

4条回答 默认 最新

  • douxiao0400 2014-04-28 04:08
    关注

    in_array() is a nice solution when checking if 1 thing matches one of multiple options

    if (!in_array($_POST['type'],array(1,2,3))){..}
    

    you should be developing with errors turned on:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波