1

大家好。我有一个联系表格 PHP 脚本。我将它用于多个站点,因为它既快速又简单。基本上,它会遍历联系人表单中的所有表单字段,无论它们是什么。做到了,所以我不必一一手动地做 POST 事情。

无论如何,我的问题很简单。以下是代码片段:

if ($thisField != "contact-submit") {
    if (($thisField != "human2"))  {
         $msg .= "<b>".$thisField ."</b>: ". $thisValue ."<br>";
    }
    }

现在,它执行此循环的问题是它会拾取所有提交的内容,包括提交按钮和我的隐藏表单字段以阻止机器人。我不想向我的客户显示这些字段。

所以我没有做这两个嵌套循环,而是想做一个

if (($thisField != "human2") or ($thisField != "contact-submit")

但它只是不起作用......我也尝试过|| 运营商也是。

我错过了什么?

4

2 回答 2

3

$thisField将永远不是human2 或不是contact-sumbit(如果是一个,则不是另一个)。你肯定是说&&

if($thisField != "human2" && $thisField != "contact-submit")
于 2012-03-18T21:47:42.367 回答
2

该表达式始终评估为真。如果将一个值与两个不同的值进行比较,它总是不等于其中至少一个。我认为您打算使用and, 或&&, 因此您可以检查该值是否不是这两个值中的任何一个。

if (($thisField != "human2") && ($thisField != "contact-submit")

或者

if (!($thisField === "human2" or $thisField === "contact-submit"))

或者

if (($thisField === "human2" or $thisField === "contact-submit") === false)
// Because you might easily overlook the exclamation mark in the second example

或使用in_array

if (! in_array($thisField, array('human2', 'contact-submit')))
// Easier add extra fields. You could stick the array in a variable too, for better readability
于 2012-03-18T21:47:04.160 回答