1

Ahoy all. I've got a contact form PHP script. I use it for multiple sites since it's quick and easy. Basically, it loops through ALL the form fields in a contact form, no matter what they are. Makes it so I don't have to manually do the POST thing one by one.

ANYWAY, my question is simple. Below is a snippet of the code:

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

Now, the problem with it doing this loop is it picks up ALL things submitted, including the SUBMIT BUTTON and my hidden form field to deter robots. I don't want to display THOSE fields to my clients.

So instead of doing these two nested loops, I was thinking of doing a

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

but it just doesn't work... I have also tried the || operator as well.

What am I missing?

PaulHanak
  • 729
  • 2
  • 9
  • 21
  • *(related)* [What does that symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Mar 18 '12 at 21:48

2 Answers2

3

$thisField will always be not human2 OR not contact-sumbit (if it's one, it's not the other). You definitely meant &&:

if($thisField != "human2" && $thisField != "contact-submit")
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @PaulHanak: Yes, `&&` returns `true` if both operands are `true`. You want it to process the field if it's "neither `human2` nor `contact-submit`", which is another way of saying "not `human2` and not `contact-submit`". So it's more like one of those "multiply two negatives and get a positive" deals. – Ry- Mar 18 '12 at 21:52
  • Thanks @minitech. I REALLLY had to break it down in my head, but I go it. hehe. Funny how the simple things stump me sometimes. – PaulHanak Mar 18 '12 at 22:41
2

That expression always evaluated to true. If you compare a value to two different values, it is always unequal to at least one of them. I think you meant to use and, or &&, so you can check if the value isn't any of those two values.

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

or

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

or

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

or use 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
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • You’re wrong. If `$thisField` equals `0`, both expressions are true. – Gumbo Mar 18 '12 at 21:50
  • Yes, so the expression as a whole still evaluates to true. My point exactly. – GolezTrol Mar 18 '12 at 21:55
  • Oh sorry, I meant they are false and not true: `0 != "human2"` is false and `0 != "contact-submit"` is false as both strings are converted to integer and also yield `0`. – Gumbo Mar 18 '12 at 21:58
  • Alright. In theory that is true, but in the context of the question, $thisField contains a form fieldname, so it should always be a string at least, even if it would contain `"0"`. – GolezTrol Mar 18 '12 at 22:01