使用if语句验证多个选项

时间:2013-05-10 02:30:37

标签: php html html5 forms

我遇到了验证字段为空还是已满的问题。下面的代码会有所帮助。

以下不是我的整个表格,只是你需要知道的部分。 HTML:

<form action="" method="post" id="all_message">
    <ul>
        <li>
            <label for="subject">Subject</label><br />
            <select id="No_dropdown" name="Dropown_select">
                <option value="Request for quote">Request for quote</option>
                <option value="Service inquiries">Service inquiries</option>
                <option value="Other">Other</option>
            </select>
        </li>
        <li>
            <input type="submit" value="Send" name="submit" />
        </li>
    </ul>
</form>

我使用此脚本对上面的选择选项进行无选择。 的Javascript

<script language="javascript" type="text/javascript">
document.getElementById("No_dropdown").selectedIndex = -1;
</script>

这不是整个PHP,只是我遇到麻烦的部分。 PHP:

$subject =      $_POST['Dropown_select'];

if (empty($name) === true || empty($email) === true || empty($phone) === true || 
    !($subject === "Other" || "Service inquiries" || "Request for quote") ||   
    empty($message) === true ) 

这是我遇到麻烦的部分:

!($subject === "Other" || "Service inquiries" || "Request for quote")

我需要该部分来查看是否选择了一个“下拉列表”,如果没有给出错误。

1 个答案:

答案 0 :(得分:5)

!($subject === "Other" || $subject === "Service inquiries" || $subject === "Request for quote")

($subject !== "Other" && $subject !== "Service inquiries" && $subject !== "Request for quote")

!in_array($subject, array("Other", "Service inquiries", "Request for quote"), true);