Simplifying my validation PHP code

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