I've used this code in the past and it has worked perfectly. It simply gathers through inputs from a form and loops through them to check that all relavent fields are filled out correctly. Only the FIRST row MUST be completley filled out and the subsequent (2-5) rows are not necessary UNLESS the user has typed something into one of those fields. However, no matter what I do it says that all the fields are required and I cant figure it out.
//NO CODE IS NEEDED UP HERE
else {
$array = array();
for ($i=1; $i<=5;$i++){
$id=$i;
$year[$i]=$_POST['year'.$i];
$recipient[$i]=addslashes($_POST['recipient'.$i]);
$value[$i]=addslashes($_POST['value'.$i]);
$intrust[$i]=$_POST['intrust'.$i];
//THIS IS WHERE IT CHECKS TO SEE THAT THE FIRST ROW IS COMPLETELY FILLED OUT
if ($i==1) {
if (empty($year[$i])) $errors .= "Please enter a year for gift #".$i.".\n";
if (empty($recipient[$i])) $errors .= "Please enter a recipient for gift #".$i.".\n";
if (empty($value[$i])) $errors .= "Please enter a dollar value for gift #".$i.".\n";
if (empty($intrust[$i])) $errors .= "Please enter whether or not for gift #".$i." is in the trust.\n";
}
//THIS IS WHERE IT LOOPS THROUGH THE REST OF THE ROWS. HOWEVER, NO MATTER WHAT, IT SAYS ALL FIELDS MUST BE FILLED OUT. WHERE IS THIS WRONG???
else {
if (!empty($year) || !empty($recipient[$i]) || !empty($value[$i]) || isset($intrust[$i])) {
if (empty($year[$i])) $errors .= "Please enter a year for gift #".$i.".\n";
if (empty($recipient[$i])) $errors .= "Please enter a recipient for gift #".$i.".\n";
if (empty($value[$i])) $errors .= "Please enter a dollar value for gift #".$i.".\n";
if (empty($intrust[$i])) $errors .= "Please enter whether or not for gift #".$i." is in the trust.\n";
}
}
//CHECK TO SEE IF ALL THESE VALUES ARE EMPTY. IF THEY ARE EMPTY, IT MOVES ON TO THE NEXT VALUE OF $i
if (($year[$i]=='') && ($recipient[$i]=='') && ($value[$i]=='') && ($intrust[$i]=='')) {
continue;
}
else{
if (empty($errors)) {
$array[$i]="INSERT INTO u_gifts (user_id,id,year,recipient,value,intrust) VALUES ('".$_SESSION['user_id']."','".$i."','".$year[$i]."','".$recipient[$i]."','".$value[$i]."','".$intrust[$i]."')";
}
}
}
}
and here is a short look at part of the form
<tr>
<td>
<input name="year1" type="text" id="year1" value="<? echo $year[1]; ?>">
</td>
<td>
<input name="recipient1" type="text" value="<? echo $recipient[1]; ?>">
</td>
<td>
<input name="value1" type="text" value="<? echo $value[1]; ?>"></td>
<td>Yes
<input type="radio" name="intrust1" value="1" <? if ($intrust[1]==1) echo "CHECKED"; ?>>
No <input type="radio" name="intrust1" value="2" <? if ($intrust[1]==2) echo "CHECKED"; ?>></td>
</tr>
This repeats up to 5. Where is this going wrong? Thanks all!