mfindlay Posted March 10, 2007 Share Posted March 10, 2007 Hi, simple question...driving me mad. I've writen the script and it all works now I want to do some validation to make sure the user enters a 'correct value', simple enough you might think, anyway what I have is 40 fields in my form which are sent to 5 different arrays for processing, so: $q1 = $_POST['q1']; $q2 = $_POST['q2']; $q3 = $_POST['q3']; //....... //then $typea = array( '1' => $q1 , '10' => $q10 , '11' => $q11 , ........ '40' => $q40); //so now I want to validate... $check = 0; foreach ($typea as $key => $value) { if ($value != 0 || $value != 2 || $value != 3) $check = 1; echo $value . " , " . $check . "<br />\n"; //to make sure values are what i think } if ($check == 1) //go back to form else //do the processing $check always returns as 1 though, even when the values are 0 , 2 or 3 help!! Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/ Share on other sites More sharing options...
papaface Posted March 10, 2007 Share Posted March 10, 2007 I think this: if ($value != 0 || $value != 2 || $value != 3) should be: if ($value == 0 || $value == 2 || $value == 3) Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/#findComment-204290 Share on other sites More sharing options...
Orio Posted March 10, 2007 Share Posted March 10, 2007 You need to change the ORs with ANDs: <?php //This: if ($value != 0 || $value != 2 || $value != 3) //Should be: if ($value != 0 && $value != 2 && $value != 3) ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/#findComment-204292 Share on other sites More sharing options...
mfindlay Posted March 10, 2007 Author Share Posted March 10, 2007 Thanks Orio, this works but I'm now even more confused than I was ??? I *thought* that by using OR I was saying if one (!=0 or 2 or 3) of the conditions were true then go back. but by using AND am I not saying that *all* of the conditions have to be true? Cheers, Mark. Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/#findComment-204302 Share on other sites More sharing options...
Orio Posted March 10, 2007 Share Posted March 10, 2007 Using or means: if the $value is not 0 or if it's not 2 or if it's not 3 then it's true. Why would this always be true? Because if $value is set to 2 for an example, $value != 0 is true so the whole condition is true. Only if $value was set to 0, 2 and 3 (which is impossible...) the condition was false. Using and means: if $value is none of the numbers (0,2,3) then the condition is true. But if it is one of the numbers, 3 for an example, then $value != 3 returns false so the whole condition is false. Get it now? Orio. Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/#findComment-204306 Share on other sites More sharing options...
mfindlay Posted March 10, 2007 Author Share Posted March 10, 2007 Yes this makes sense, thanks. maybe I should go to 'florists school', this computer lark's not for me Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/42118-solved-form-validation/#findComment-204315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.