Simplifying my validation PHP code

Thanks for all your help, am narrowing it down and now getting

"Warning: Invalid argument supplied for foreach() in /here/is/my/path/form.php on line 101"

For this bit of code:
Code:
$error['item'][$key] = false; 
foreach($_POST['item'] as $key => $value) {
     if (strlen($value < 4)) { 
        $error['item'][$key] = true; 
         $print_again = true;
    }
}

I noticed that no matter what i do it always appears as red even if i have selected an item?
 
Thanks for all your help, am narrowing it down and now getting

"Warning: Invalid argument supplied for foreach() in /here/is/my/path/form.php on line 101"

For this bit of code:
Code:
$error['item'][$key] = false; 
foreach($_POST['item'] as $key => $value) {
     if (strlen($value < 4)) { 
        $error['item'][$key] = true; 
         $print_again = true;
    }
}
I noticed that no matter what i do it always appears as red even if i have selected an item?

That error means $_POST['item'] doesn't contain an array when you get to the loop.
 
Any idea why that might be happening? My form is below, and it happens whether i select a value or not and never validates...

Code:
<?

for($x = 0; $x < 2; $x++) {
// item
	echo ($error['item'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Item</strong> (*)</td>';
	echo '<td><select name="item[]">';
	if($_POST['item'][$x]) echo '<option>' . $_POST['item'][$x] . '</option>';
	include('products.txt');
	echo '</select></td></tr>';

// quantity
	echo ($error['quantity'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Quantity</strong> (*)</td>';
	echo '<td><select name="quantity[]">';
	if($_POST['quantity'][$x]) echo '<option>' . $_POST['quantity'][$x] . '</option>';
	include('quantity.txt');
	echo '</select></td></tr>';

// reason
	echo ($error['reason'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>';
	echo '<td><textarea name="reason[]" rows="5" style="width:500px;">';
	if($_POST['reason'][$x]) echo $_POST['reason'][$x];
	echo '</textarea></td></tr>';

// type
	echo ($error['type'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="type[' . $x . '][]" value="Replacement" ';
	if($_POST['type'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="type[' . $x . '][]" value="Enhancement" ';
	if($_POST['type'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';

// oldassets
	echo ($error['asset'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Old assets?</strong> (*)</td>';
	echo '<td><input type="text" name="asset[]" value="';
	if($_POST['asset'][$x]) echo $_POST['asset'][$x];
	echo '" style="width:200px;" />';
	echo '</td></tr>';
	echo '<tr><td colspan="2"><hr></td></tr>';
}

?>

I've looked through everything and re-copied/pasted just in case i had anything wrong but not making any progress? :(
 
print_r shows that it definately has a value in item:

Array ( [submit] => null [username] => [address] => Mr Test, ETC, ETC, ETC [lognumber] => 654565 [contactname] => JOHN [contactmail] => [email protected] [item] => Array ( [0] => Brother LT-5300 Lower Tray (for 5240) [1] => HP Colour Laserjet 5400 ) [quantity] => Array ( [0] => 1 [1] => 2 ) [reason] => Array ( [0] => 111111111111111111 [1] => 22222222222222222 ) [type] => Array ( [0] => Array ( [0] => Replacement ) [1] => Array ( [0] => Enhancement ) ) [asset] => Array ( [0] => 1111 [1] => 2222 ) [notes] => [Submit] => Submit ERF )
Array ( [submit] => null [username] => [address] => Mr Test, ETC, ETC, ETC [lognumber] => 654565 [contactname] => JOHN [contactmail] => [email protected] [item] => Array ( [0] => Brother LT-5300 Lower Tray (for 5240) [1] => HP Colour Laserjet 5400 ) [quantity] => Array ( [0] => 1 [1] => 2 ) [reason] => Array ( [0] => 111111111111111111 [1] => 22222222222222222 ) [type] => Array ( [0] => Array ( [0] => Replacement ) [1] => Array ( [0] => Enhancement ) ) [asset] => Array ( [0] => 1111 [1] => 2222 ) [notes] => [Submit] => Submit ERF )

Although im not sure why it displays it twice!
 
Please help!

Ok i'm really scratching my head now, here is my complete validaion code:

Code:
<? 
//validation
function error_bool($error, $field) { 
         if($error[$field]) { 
             print("<td style=color:red>"); 
         } 
        else { 
            print("<td>"); 
        } 
    } 

function show_form() { 
global $HTTP_POST_VARS, $print_again, $error; 

} 
if(isset($_POST["Submit"])) { 
    check_form(); 
} else { 
    show_form(); 
} 

function check_form() 
{ 
global $HTTP_POST_VARS, $error, $print_again; 

// validate constant fields
$error['address'] = false; 
    if (strlen($_POST['address']) < 10) { 
        $error['address'] = true; 
         $print_again = true; 
    } 
$error['lognumber'] = false; 
    if (strlen($_POST['lognumber']) < 6) { 
        $error['lognumber'] = true; 
         $print_again = true; 
    } 
$error['contactname'] = false; 
    if (strlen($_POST['contactname']) < 3) { 
        $error['contactname'] = true; 
         $print_again = true; 
    } 

// validate the items
$error['theitem'][$key] = false; 
foreach($_POST['theitem'] as $key => $value) {
     if (strlen($value < 5)) { 
        $error['theitem'][$key] = true; 
         $print_again = true;
    }
}

$error['quantity'][$key] = false; 
foreach($_POST['quantity'] as $key => $value) {
     if (is_numeric($value)) { 
	} else {
        $error['quantity'][$key] = true; 
         $print_again = true;
    }
}

$error['reason'][$key] = false; 
foreach($_POST['reason'] as $key => $value) {
     if (strlen($value < 10)) { 
        $error['reason'][$key] = true; 
         $print_again = true;
    }
}

$error['thetype'][$key] = false; 
foreach($_POST['thetype'] as $key => $value) {
     if ($value=="") { 
        $error['thetype'][$key] = true; 
         $print_again = true;
    }
}

foreach($_POST['asset'] as $key => $value) {
 	   if ($_POST["asset"][$key]=="") {
        $error['asset'][$key] = true; 
         $print_again = true; 
    }
}

} else {

(my bit of processing which is yet to be arrayed!)

And here is my complete form:

Code:
<!-- form start -->

<form action="" method="post"> 
<input type="hidden" name="submit" value="null" />
<input type="hidden" name="username" value="<? global $USERINFO; echo $USERINFO['name']; ?>" />
  <table width="800" border="0" cellspacing="0" cellpadding="2"> 

<!-- form constants -->
	<tr><td colspan="2"><hr></td></tr>
    <tr> 
     <td><strong>Submitting as</strong></td> 
      <td><? global $USERINFO; echo $USERINFO['name']; ?></td>
    </tr>
    <tr> 
     <?php error_bool($error, "address"); ?><strong>Deliver to</strong> (*)</td> 
      <td><select id="address" name="address" size="1" style="width:600px;"><? if($_POST["address"]<>"") { echo '<option>' . $_POST["address"] . '</option>'; } ?><? include('addresses.txt'); ?></select>
    </tr> 
    <tr> 
      <?php error_bool($error, "lognumber"); ?><strong>Log number</strong> (*)</td><td><input type="text" name="lognumber" value="<? echo $_POST["lognumber"]; ?>" style="width:100px;" /> (e.g. 234567)</td>
    </tr> 
    <tr> 
	<?php error_bool($error, "contactname"); ?><strong>Contact name</strong> (*)</td><td><input class="edit" type="text" name="contactname" value="<? echo $_POST["contactname"]; ?>" style="width:200px;" /> (name of contact at surgery who is aware of this order)</td>
	</tr>
	<tr>
      <?php error_bool($error, "contactmail"); ?><strong>Contact email</strong> (*)</td> 
      <td><input name="contactmail" type="text" id="mail" value="<? echo $_POST["contactmail"]; ?>"> (email address of above contact so a confirmation email can be sent)</td> 
    </tr>
	<tr><td colspan="2"><hr></td></tr>


<?

for($x = 0; $x < 2; $x++) {
// item
	echo ($error['theitem'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Item</strong> (*)</td>';
	echo '<td><select name="theitem[]">';
	if($_POST['theitem'][$x]) echo '<option>' . $_POST['theitem'][$x] . '</option>';
	include('products.txt');
	echo '</select></td></tr>';

// quantity
	echo ($error['quantity'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Quantity</strong> (*)</td>';
	echo '<td><select name="quantity[]">';
	if($_POST['quantity'][$x]) echo '<option>' . $_POST['quantity'][$x] . '</option>';
	include('quantity.txt');
	echo '</select></td></tr>';

// reason
	echo ($error['reason'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>';
	echo '<td><textarea name="reason[]" rows="5" style="width:500px;">';
	if($_POST['reason'][$x]) echo $_POST['reason'][$x];
	echo '</textarea></td></tr>';

// type
	echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
	if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
	if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';

// oldassets
	echo ($error['asset'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Old assets?</strong> (*)</td>';
	echo '<td><input type="text" name="asset[]" value="';
	if($_POST['asset'][$x]) echo $_POST['asset'][$x];
	echo '" style="width:200px;" />';
	echo '</td></tr>';
	echo '<tr><td colspan="2"><hr></td></tr>';
}

?>
	<tr>
	<?php error_bool($error, "notes"); ?><strong>Additonal notes</strong></td>
	<td><textarea class="edit" name="notes" rows="5" style="width:500px;" ><? echo $_POST["notes"]; ?></textarea></td>
    </tr>
	<tr> 
      <td> </td> 
      <td><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
	<tr><td colspan="2"><hr></td></tr>
  </table> 
</form>

The error i get is "Warning: Invalid argument supplied for foreach() in /my/path/form.php on line 101" if i have the "theitem" validation still in there and try submitting the page, and i get the same message for the "thetype" validation also. If i remove them two the form processes OK.

In summary, if i try and validate either "theitem" or "thetype" it won't do it.

Please, please help, i really need it!
 
Have changed my error reporting to all and now get these:

Notice: Undefined variable: key in /my/form.php on line 75

Warning: Invalid argument supplied for foreach() in /my/form.php on line 101

Notice: Undefined offset: 0 in /my/formhtml.php on line 58

Notice: Undefined index: notes in /my/form.php on line 4

Does that make any difference?!
 
Last edited:
Still not found any answers, really not sure what else to do, so here's absolutely all the information i have in the hope that somebody can help me.

The problem:
If i select an "Item" the form doesn't think that i have and will not let you submit the form. I have the opposite problem with the "Type" radio buttons as if i don't select one it doesn't care and doesn't mark it as needing to be filled out by turning the text red! Please can somebody help.

The URL:
http://richl.com/gpt/erf/erf_form.php

The code:

Code:
<? 
//validation
function error_bool($error, $field) { 
         if($error[$field]) { 
             print("<td style=color:red>"); 
         } 
        else { 
            print("<td>"); 
        } 
    } 

function show_form() { 
global $HTTP_POST_VARS, $print_again, $error; 

include 'erf_html.php';

} 
if(isset($_POST["Submit"])) { 
    check_form(); 
} else { 
    show_form(); 
} 

function check_email_address($contactmail) { 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $contactmail)) { 
    return false; 
  } 
  $email_array = explode("@", $contactmail); 
  $local_array = explode(".", $email_array[0]); 
  for ($i = 0; $i < sizeof($local_array); $i++) { 
     if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { 
      return false; 
    } 
  } 
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { 
    $domain_array = explode(".", $email_array[1]); 
    if (sizeof($domain_array) < 2) { 
        return false;
    } 
    for ($i = 0; $i < sizeof($domain_array); $i++) { 
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { 
        return false; 
      } 
    } 
  } 
  return true; 
} 


function check_form() 
{ 
global $HTTP_POST_VARS, $error, $print_again; 

// validate constant fields
$error['address'] = false; 
    if (strlen($_POST['address']) < 4) { 
        $error['address'] = true; 
         $print_again = true; 
    } 
$error['lognumber'] = false; 
    if (strlen($_POST['lognumber']) < 6) { 
        $error['lognumber'] = true; 
         $print_again = true; 
    } 
$error['contactname'] = false; 
    if (strlen($_POST['contactname']) < 3) { 
        $error['contactname'] = true; 
         $print_again = true; 
    } 

$error['theitem'][$key] = false; 
foreach($_POST['theitem'] as $key => $value) {
     if ($value=="n/a") { 
	} else {
        $error['theitem'][$key] = true; 
         $print_again = true;
    }
}

$error['quantity'][$key] = false; 
foreach($_POST['quantity'] as $key => $value) {
     if (is_numeric($value)) { 
	} else {
        $error['quantity'][$key] = true; 
         $print_again = true;
    }
}

$error['reason'][$key] = false; 
foreach($_POST['reason'] as $key => $value) {
     if (strlen($value < 10)) { 
        $error['reason'][$key] = true; 
         $print_again = true;
    }
}

$error['thetype'][$key] = false; 
foreach($_POST['thetype'] as $key => $value) {
     if ($value=="") {
        $error['thetype'][$key] = true; 
         $print_again = true;
    }
}

foreach($_POST['asset'] as $key => $value) {
 	   if ($_POST["asset"][$key]=="") {
        $error['asset'][$key] = true; 
         $print_again = true; 
    }
}

// validate email address
    if (!check_email_address($_POST['contactmail'])) { 
        $error['contactmail'] = true; 
         $print_again = true; 
    } 
     if ($print_again) { 
         show_form(); 
        
       } else { 

// get the PCT contact name from the address
	$theaddress = $_POST['address'];

if (strpos($theaddress, 'ESDW') !== false) {
  	$subto = 'Richard West';
  	$subtopct = 'ESDW';
} else if (strpos($theaddress, 'HR') !== false) {
  	$subto = 'Teresa Freeman';
  	$subtopct = 'HR';
} else if (strpos($theaddress, 'BH') !== false) {
  	$subto = 'Natasha Darby';
  	$subtopct = 'BH';
} else if (strpos($theaddress, 'WSX') !== false) {
  	$subto = 'Carl Bolger / Tina Hayes / Elaine Wakeham / Tracy Witham';
  	$subtopct = 'WSX';
}

// get the doctors name from the address
$getgpname = $_POST['address'];
$getgpname = substr($getgpname,strpos($getgpname,'-')+1);
$getgpname = substr($getgpname,0,strpos($getgpname,','));
$getgpname = trim($getgpname);

// define variables
$erfref = DATE('Ymd-His') . " " . $getgpname . " ERF";
$date = DATE('d/m/Y');
$subby = $_POST['username'];
$deladdress = $_POST['address'];
$logno = $_POST['lognumber'];
$contactname = $_POST['contactname'];
$contactmail = $_POST['contactmail'];
$notes = $_POST['notes'];

// if the form is submitted, show some output
if ($_POST['submit'] == true)
						{


foreach($_POST['theitem'] as $key => $value) {
     if ($value<>"") {
        $itemlist = $value . '<br \>';
    }
}
foreach($_POST['quantity'] as $key => $value) {
     if ($value<>"") {
        $itemlist = $value . '<br \>';
    }
}
foreach($_POST['reason'] as $key => $value) {
     if ($value<>"") {
        $itemlist = $value . '<br \>';
    }
}
foreach($_POST['thetype'] as $key => $value) {
     if ($value<>"") {
        $itemlist = $value . '<br \>';
    }

}


}
//	header('Location: http://10.179.255.10/doku.php?id=erf:submitted');
	show_form();
       } 
	echo "<strong>** Please fill out the required fields to proceed.</strong>";
} 

?>

And here is the code for my form, found in erf_html.php:


Code:
<!-- form start -->

<form action="" method="post"> 
<input type="hidden" name="submit" value="null" />
<input type="hidden" name="username" value="<? global $USERINFO; echo $USERINFO['name']; ?>" />
  <table width="800" border="0" cellspacing="0" cellpadding="2"> 

<!-- form constants -->
	<tr><td colspan="2"><hr></td></tr>
    <tr> 
     <td><strong>Submitting as</strong></td> 
      <td><? global $USERINFO; echo $USERINFO['name']; ?></td>
    </tr>
    <tr> 
     <?php error_bool($error, "address"); ?><strong>Deliver to</strong> (*)</td> 
      <td><select id="address" name="address" size="1" style="width:600px;"><? if($_POST["address"]<>"") { echo '<option>' . $_POST["address"] . '</option>'; } ?><? include('addresses.txt'); ?></select>
    </tr> 
    <tr> 
      <?php error_bool($error, "lognumber"); ?><strong>Log number</strong> (*)</td><td><input type="text" name="lognumber" value="<? echo $_POST["lognumber"]; ?>" style="width:100px;" /> (e.g. 234567)</td>
    </tr> 
    <tr> 
	<?php error_bool($error, "contactname"); ?><strong>Contact name</strong> (*)</td><td><input class="edit" type="text" name="contactname" value="<? echo $_POST["contactname"]; ?>" style="width:200px;" /> (name of contact who is aware of this order)</td>
	</tr>
	<tr>
      <?php error_bool($error, "contactmail"); ?><strong>Contact email</strong> (*)</td> 
      <td><input name="contactmail" type="text" id="mail" value="<? echo $_POST["contactmail"]; ?>"> (email address of above contact so a confirmation email can be sent)</td> 
    </tr>
	<tr><td colspan="2"><hr></td></tr>


<?

for($x = 0; $x < 2; $x++) {
// item
	echo ($error['theitem'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Item</strong> (*)</td>';
	echo '<td><select name="theitem[]">';
	if($_POST['theitem'][$x]) echo '<option>' . $_POST['theitem'][$x] . '</option>';
	include('products.txt');
	echo '</select></td></tr>';

// quantity
	echo ($error['quantity'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Quantity</strong> (*)</td>';
	echo '<td><select name="quantity[]">';
	if($_POST['quantity'][$x]) echo '<option>' . $_POST['quantity'][$x] . '</option>';
	include('quantity.txt');
	echo '</select></td></tr>';

// reason
	echo ($error['reason'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>';
	echo '<td><textarea name="reason[]" rows="5" style="width:500px;">';
	if($_POST['reason'][$x]) echo $_POST['reason'][$x];
	echo '</textarea></td></tr>';

// type
	echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
	if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
	if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';

// oldassets
	echo ($error['asset'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Old assets?</strong> (*)</td>';
	echo '<td><input type="text" name="asset[]" value="';
	if($_POST['asset'][$x]) echo $_POST['asset'][$x];
	echo '" style="width:200px;" />';
	echo '</td></tr>';
	echo '<tr><td colspan="2"><hr></td></tr>';
}

?>
	<tr>
	<?php error_bool($error, "notes"); ?><strong>Additonal notes</strong><br \></td>
	<td><textarea class="edit" name="notes" rows="5" style="width:500px;" ><? echo $_POST["notes"]; ?></textarea></td>
    </tr>
	<tr> 
      <td> </td> 
      <td><input type="submit" name="Submit" value="Submit ERF"></td> 
    </tr> 
	<tr><td colspan="2"><hr></td></tr>
  </table> 
</form>
 
Last edited:
Ok no problem, seem to have solved the "theitem" issue, but its still not validating the "thetype" radio boxes, can anyone see any problems with these bits of code:


In erf_form.php:
Code:
$error['thetype'][$key] = false; 
foreach($_POST['thetype'] as $key => $value) {
     if ($value<>"") {
        $error['thetype'][$key] = true; 
         $print_again = true;
    }
}

In erf_html.php:
Code:
// type
	echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
	if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
	if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';
 
Im sure the problem is with this bit:

Code:
foreach($_POST['thetype'] as $key => $value) {

In the validation, as the "name" of the radio form inputs are thetype[$x][] rather than thetype[] but im not sure how to add the [$x] bit on to validate the correct values!
 
Well, i seem to have improved as it will now "remember" the options you selected when POSTing the form, but the validation still doesn't seem to want to stop you if neither of the options are selected. Here's my latest code:

Form:
Code:
// type
	echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
	if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
	if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';

Validation:
Code:
$error['thetype'][$key] = false; 
foreach($_POST['thetype'][$x][0] as $key => $value) {
     if (strlen($value < 5)) {
        $error['thetype'][$key] = true; 
         $print_again = true;
    }
}
 
Thanks for your response, am making progress using this:

Code:
$error['thetype'][$key] = false;
	for($x = 0; $x < 2; $x++) {
   	 if(!$_POST['thetype'][$x][0]) {
        $error['thetype'][$key] = true;
         $print_again = true;
	}
}

For some strange reason if i loop the form and this twice, the first "Type" on the form is ignored (will not go red if an option isnt selected) but the second "Type" on the form does! Any idea's?

Edit: OK, if i loop the form and the validation 5 times, it actually appears that all but the last "Type" will go red if neither of the radio boxes are selected, all the rest stay black...weird?
 
Last edited:
unrelated, but lose this line of code on each field....

Code:
$error['thetype'][$key] = false;

it doesn't do anything. also the $print_again thing is pointless. just checking if $error exists or not should work instead.

as for your problem, print_r is your friend.....

Code:
print_r($_POST['thetype']);

should give you some idea what you're looking for. :)
 
Here's what print_r gives, but it looks as it should do to me, nothing particularly special about the last one...and yet still its only the last value that gets the red text?

Code:
Array ( [0] => Array ( [0] => Replacement ) [1] => Array ( [0] => Replacement ) [2] => Array ( [0] => Replacement ) [3] => Array ( [0] => Enhancement ) [4] => Array ( [0] => Enhancement ) )

Form code:
Code:
<?

for($x = 0; $x < 5; $x++) {
// item
	echo ($error['theitem'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Item</strong> (*)</td>';
	echo '<td><select name="theitem[]">';
	if($_POST['theitem'][$x]) echo '<option>' . $_POST['theitem'][$x] . '</option>';
	include('products.txt');
	echo '</select></td></tr>';

// quantity
	echo ($error['quantity'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Quantity</strong> (*)</td>';
	echo '<td><select name="quantity[]">';
	if($_POST['quantity'][$x]) echo '<option>' . $_POST['quantity'][$x] . '</option>';
	include('quantity.txt');
	echo '</select></td></tr>';

// reason
	echo ($error['reason'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>';
	echo '<td><textarea name="reason[]" rows="5" style="width:500px;">';
	if($_POST['reason'][$x]) echo $_POST['reason'][$x];
	echo '</textarea></td></tr>';

// type
	echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
	if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
	if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo $_POST['thetype'][$x][0];
	echo '</td></tr>';

// oldassets
	echo ($error['asset'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Old assets?</strong> (*)</td>';
	echo '<td><input type="text" name="asset[]" value="';
	if($_POST['asset'][$x]) echo $_POST['asset'][$x];
	echo '" style="width:200px;" />';
	echo '</td></tr>';
	echo '<tr><td colspan="2"><hr></td></tr>';
}

?>

And validation code is now:
Code:
$error['thetype'][$key] = false;
	for($x = 0; $x < 5; $x++) {
   	 if(strlen($_POST['thetype'][$x][0] < 8)) {
        $error['thetype'][$key] = true;
         $print_again = true;
	}
}

I really suck at this, i've literally spent half the day trying to work this out...please, please, don't give up one me!
 
Ok again im making progress, if i use this:

Code:
$error['thetype'][$x] = false;
	for($x = 0; $x < 5; $x++) {
   	 if(strlen($_POST['thetype'][$x][0] < 7)) {
        $error['thetype'][$x] = true;
         $print_again = true;
	}
}

It makes ALL of the "Item" bits red so using ['thetype'][$x] obviously works for that. Now the only problem i have is why when i select one it thinks it isn't validated? For example, if i choose a radio option it should be validated as the value is "Replacement" or "Enhancement" which is more than 7 letters (the qualifier above)...
 
Unbelieveable. Thank you. Now after i sleep for a long time i need to work out how to get all those different bits of the array out! Thanks for your help!
 
Back
Top Bottom