在PHP

时间:2017-05-18 17:36:41

标签: php forms select

我尝试根据SELECT框和用户输入创建搜索选项(两者都是必需的)。但是在下面的代码中,正确和错误的输入都显示为错误的输入。有人可以解释一下代码中的错误。

这是HTML

<form action="" method="POST">
<select name="selectOpt">
                  <option>Select a list</option>
                  <option value="one">ID</option>
                  <option value="delName">Dealer Name</option>
                  <option value="medName">Medical Name</option>

            </select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>

            <button type="submit" name="submit">search</button>
 </form>

PHP

if(isset($_POST['submit'])){

    if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){


        if($_POST['selectOpt']=='one'){
            $id = $_POST['selectOpt'];

            if (!preg_match("/^[0-9]*$/",$id)){
                echo "not valid";

            }else{
                echo "valid";
            }
        }
    }else{
        echo "Enter Value";
    }



}

2 个答案:

答案 0 :(得分:0)

您的模式/^[0-9]*$/,写入需要零个或多个数字的序列,并不匹配您的选择框的任何可能值,这些都是字母字符串。

答案 1 :(得分:0)

<form action="save.php" method="POST">
<select name="selectOpt">
                  <option>Select a list</option>
                  <option value="one">ID</option>
                  <option value="delName">Dealer Name</option>
                  <option value="medName">Medical Name</option>

            </select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>

            <button type="submit" name="submit">search</button>
 </form>

<?php

if(isset($_POST['submit'])){

    if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){


        if($_POST['selectOpt'] == "one"){

            $id = $_POST['selectOpt'];


            if (preg_match("/^[0-9]*$/",$id)){

                echo "not valid";

            }else{
                echo "valid";

                   echo $input = $_POST['uinput'];
            }
        }
        else{

          echo "PLEASE Enter Proper and Valid Search  ";

        }

    }
  else{
        echo "PLEASE Enter Value ";
        }



}

?>