Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array problem HELP please :)

Status
Not open for further replies.

troinfo

Programmer
Feb 7, 2002
2
US
I'm triyng to validate form fields from user input. The code works (somewhat) meaning i get no errors but it seems to only validate the last field as opposed to stopping on the the field that an error is found. I'm kinda new to php with a little asp. my test code is as follows:

<?

if ($submit == submit) {

$field = array(&quot;test&quot;, &quot;test1&quot;, &quot;test2&quot; ,&quot;test3&quot;, &quot;test4&quot;, &quot;test5&quot;);

for ($i = 0; $i < count($field); $i++) {


if (!preg_match(&quot;/^[a-zA-Z0-9]+$/&quot;,${$field[$i]})) {


} else {


if (${$field[$i]} = true) {

$msg = &quot;good&quot;;

} else {

$msg =&quot;not good&quot;;


}
}
}
}

?>

<form method=post action=&quot;test.php&quot;>

<p>&nbsp;<? echo &quot;$msg&quot;; ?>

<p> input test: <input type=text name=&quot;test&quot; size=25>

<p> input test: <input type=text name=&quot;test1&quot; size=25>

<p> input test: <input type=text name=&quot;test2&quot; size=25>

<p> input test: <input type=text name=&quot;test3&quot; size=25>

<p> input test: <input type=text name=&quot;test4&quot; size=25>

<p> input test: <input type=text name=&quot;test5&quot; size=25>

<p><input type=submit name=&quot;submit&quot; value=submit>

</form>
 
i don't understand what you want to validate. Is a little mess in your head.

Why you do the first if without nothing in the THEN clause?

after, when you do if($$field[$i] = true) you are SETTING the vars test,test1, ... with true value, and not comparing it to true. To do this you must put == instead of =.


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i want to check each charater in a field then move to the next field and repeat. If an error shows up in a field it is supposed to stop the loop.
 
hello,

why dont u use 'break' to get out of the loop when error is found?

so if (${$field[$i]} == true) {

$msg = &quot;good&quot;;

} else {

$msg =&quot;not good&quot;;
echo $msg;
break;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top