Click to See Complete Forum and Search --> : For loop for checking a form


haynbrian
11-25-2003, 08:23 PM
Hello all.

I am working with a form where there is a text field, two checkboxes, and another text field. Both text fields need to be required, and either of the check boxes have to be checked.

Below is an example of the form that I have created.


<form name='merch_frm' action='merch.php' method='post'>
<input name="txtOne" type="text" id="txtOne">
<br/>
Check One:
<input name="chkOne" type="checkbox" id="chkOne" value="checkbox">
<br/>
Check Two:
<input name="chkTwo" type="checkbox" id="chkTwo" value="checkbox">
<br/>
Input:
<input name="txtTwo" type="text" id="txtTwo"><br/>

<input type="submit" name="Submit" value="Submit">
<form>


I've seen several ways that forms get evaluated, and I would like my error message to list the errors according to what was incorrect, not one at a time. For example:

*error1 (if applicable)
*error2 (if applicable)
*error3 (if applicable)

I have tried creating a for loop in PHP to do this, but it's not doing what I want. Below is what I have:


<?php
for ($i=0; $i<=3; $i++) {

if ($txtOne=="") {
echo 'wrong 1';
}

else if ($chkOne=="" || $chkTwo=="") {
echo 'wrong2';
}

else if ($txtTwo=="") {
echo 'wrong3';
break;
}

}


Can anyone tell me what I'm doing wrong?


Thanks,

Brian

dreamcatcher
11-26-2003, 05:43 PM
Hi Brian,

An if else statement will only return true once. So if the first part of the code is true, then it won`t continue and so on.

You can also lose the for loop. Just try if statements without the else clause:


if ($txtOne=="") {
echo 'wrong 1';
}

if ($chkOne=="" || $chkTwo=="") {
echo 'wrong2';
}

if ($txtTwo=="") {
echo 'wrong3';


See if that works!

haynbrian
11-27-2003, 02:20 PM
That works great, thanks!

Brian