Jump to content

[SOLVED] Form validation


mfindlay

Recommended Posts

 

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!!

Link to comment
https://forums.phpfreaks.com/topic/42118-solved-form-validation/
Share on other sites

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.