Simplifying my validation PHP code

Associate
Joined
29 Dec 2004
Posts
2,253
I'm writing a form that uses some PHP validation before it is processed, here is the code i'm using to validate one "set" of fields, for ordering an item:

Code:
if($_POST['item2']<>"n/a") {
$error['item2'] = false; 
    if (strlen($_POST['item2']) < 4) { 
        $error['item2'] = true; 
         $print_again = true; 
    } 
$error['quantity2'] = false; 
    if (is_numeric($_POST['quantity2'])) { 
	} else {
        $error['quantity2'] = true; 
         $print_again = true; 
    }
$error['reason2'] = false; 
    if (strlen($_POST['reason2']) < 10) { 
        $error['reason2'] = true; 
         $print_again = true; 
    } 
$error['type2'] = false; 
    if ($_POST["type2"]=="") { 
        $error['type2'] = true; 
         $print_again = true; 
    } 
if($_POST['type2']=="Replacement") {
    if ($_POST["asset2"]=="") {
        $error['asset2'] = true; 
         $print_again = true; 
    }
  }

The only thing is, i also have 5 of these "sets" of fields in total, so currently have this:

Code:
if($_POST['item2']<>"n/a") {
$error['item2'] = false; 
    if (strlen($_POST['item2']) < 4) { 
        $error['item2'] = true; 
         $print_again = true; 
    } 
$error['quantity2'] = false; 
    if (is_numeric($_POST['quantity2'])) { 
	} else {
        $error['quantity2'] = true; 
         $print_again = true; 
    }
$error['reason2'] = false; 
    if (strlen($_POST['reason2']) < 10) { 
        $error['reason2'] = true; 
         $print_again = true; 
    } 
$error['type2'] = false; 
    if ($_POST["type2"]=="") { 
        $error['type2'] = true; 
         $print_again = true; 
    } 
if($_POST['type2']=="Replacement") {
    if ($_POST["asset2"]=="") {
        $error['asset2'] = true; 
         $print_again = true; 
    }
  }

if($_POST['item3']<>"n/a") {
$error['item3'] = false; 
    if (strlen($_POST['item3']) < 4) { 
        $error['item3'] = true; 
         $print_again = true; 
    } 
$error['quantity3'] = false; 
    if (is_numeric($_POST['quantity3'])) { 
	} else {
        $error['quantity3'] = true; 
         $print_again = true; 
    }
$error['reason3'] = false; 
    if (strlen($_POST['reason3']) < 10) { 
        $error['reason3'] = true; 
         $print_again = true; 
    } 
$error['type3'] = false; 
    if ($_POST["type3"]=="") { 
        $error['type3'] = true; 
         $print_again = true; 
    } 
if($_POST['type3']=="Replacement") {
    if ($_POST["asset3"]=="") {
        $error['asset3'] = true; 
         $print_again = true; 
    }
  }

// ..etc etc etc for $variable4, 5.

Is there any way of simplifying it so i dont have to have the code over and over again with just a different number?!
 
use arrays in your form.

Code:
<input type="text" name="item[]" />
<input type="text" name="quantity[]" />
//etc

put that inside a loop to display how many items you want rather than having
item1, item2 etc....

then when the form posts, the first item in the form will be $_POST['item'][0]
$_POST['item'][1] will be the 2nd and so on....

and to validate them all, use a loop again....

Code:
foreach($_POST['item'] as $key => $value) {
     if (strlen($value < 4) { 
        $error['item'][$key] = true; 
         $print_again = true; 
    }
}
 
Seem's to give me a blank page when i try and use it, turned friendly HTTP error messages on but not seeing anything (can't remember if i may have turned something off in PHP.ini), i was using this:

Code:
foreach($_POST['item'] as $key => $value) {
     if (strlen($value < 4) { 
        $error['item'][$key] = true; 
         $print_again = true;
    }
}
 
oops, missing a closing bracket on the strlen bit. of course you should have error reporting on when doing your development.... :p
 
Quick one, here's part of my form:

Code:
	<tr>
	<?php error_bool($error, "item1"); ?><strong>Item</strong> (*)</td>
	<td><select name="item[]" size="1" style="width:px;"><? if($_POST["item1"]<>"") { echo '<option>' . $_POST["item1"] . '</option>'; } ?><? include('products.txt'); ?></select></td>
    </tr>
	<tr>
	<?php error_bool($error, "quantity1"); ?><strong>Quantity</strong> (*)</td>
	<td><select name="quantity[]" size="1" style="width:px;"><? if($_POST["quantity1"]<>"") { echo '<option>' . $_POST["quantity1"] . '</option>'; } ?><? include('quantity.txt'); ?></td>
    </tr>
	<tr>
	<?php error_bool($error, "reason1"); ?><strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>
	<td><textarea class="edit" name="reason[]" rows="5" style="width:500px;"><? echo $_POST["reason1"]; ?></textarea></td>
    </tr>
	<tr>
	<?php error_bool($error, "type1"); ?><strong>Type</strong> (*)</td>
	<td><input type="radio" name="type[]" value="Replacement"<? if($_POST["type1"]=="Replacement") { echo ' checked="checked"'; } ?>>&nbsp;Replacement</td></tr><tr>
<td> </td><td><input type="radio" name="type[]" value="Enhancement"<? if($_POST["type1"]=="Enhancement") { echo ' checked="checked"'; } ?>>&nbsp;Enhancement</td>
    </tr>
	<tr>
	<?php error_bool($error, "asset1"); ?><strong>Old assets?</strong></td>
	<td><input type="text" name="asset[]" value="<? echo $_POST["asset1"]; ?>" style="width:200px;" /></td>
    </tr>


And a bit of the validation, which makes sense of the bit above:

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; 
?>

How can i get the "keys" to go onto the validation bit "quantity1" etc? :confused:
 
have the form display inside a loop as i mentioned in my first post.....

Code:
for($x = 0; $x < 5; $x++) {
    if($error['item'][$x]).....

now you can access the array. :)
 
That looks insane, so something like this for me:

Code:
<?

for($x = 0; $x < 5; $x++) {

	<tr>
	<?php error_bool($error, "item[]"); ?><strong>Item</strong> (*)</td>
	<td><select name="item[]" size="1" style="width:px;"><? if($_POST["item[]"]<>"") { echo '<option>' . $_POST["item[]"] . '</option>'; } ?><? include('products.txt'); ?></select></td>
    </tr>
	<tr>
	<?php error_bool($error, "quantity[]"); ?><strong>Quantity</strong> (*)</td>
	<td><select name="quantity[]" size="1" style="width:px;"><? if($_POST["quantity[]"]<>"") { echo '<option>' . $_POST["quantity[]"] . '</option>'; } ?><? include('quantity.txt'); ?></td>
    </tr>
	<tr>
	<?php error_bool($error, "reason[]"); ?><strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>
	<td><textarea class="edit" name="reason[]" rows="5" style="width:500px;"><? echo $_POST["reason[]"]; ?></textarea></td>
    </tr>
	<tr>
	<?php error_bool($error, "type[]"); ?><strong>Type</strong> (*)</td>
	<td><input type="radio" name="type[]" value="Replacement"<? if($_POST["type[]"]=="Replacement") { echo ' checked="checked"'; } ?>>&nbsp;Replacement</td></tr><tr>
<td> </td><td><input type="radio" name="type[]" value="Enhancement"<? if($_POST["type[]"]=="Enhancement") { echo ' checked="checked"'; } ?>>&nbsp;Enhancement</td>
    </tr>
	<tr>
	<?php error_bool($error, "asset[]"); ?><strong>Old assets?</strong></td>
	<td><input type="text" name="asset[]" value="<? echo $_POST["asset[]"]; ?>" style="width:200px;" /></td>
    </tr>
	<tr><td colspan="2"><hr></td></tr>

}

?>

I really appreciate your help but am a little confused...any additional examples would be good if the above is wrong! (which im sure it is)
 
i've lost the error_bool function....
Code:
for($x = 0; $x < 5; $x++) {
	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>';
	//etc
}

incase you're wondering what this is.....

Code:
echo ($error['item'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';

it's called a ternary operator and it's just a quicker way of doing

Code:
if($error['item'][$x]) {
    echo '<tr><td style="color: red;">';
} else {
    echo '<tr><td>';
}
 
Last edited:
That displays a white page, noticed there was a ; missing at the end of the third echo but still showing a white page, would you pretty please mind double checking it?!
 
richieboy, turn on error reporting at the top of your script and PHP will give you decent hints as to what the problem is (inc a line number) rather than a blank page.

Code:
// tell php to report all errors, including strict
error_reporting(E_ALL | E_STRICT);
// tell php to output errors to the screen instead of just logging them
ini_set('display_errors', 1);
 
Well i'm all good apart from this bit:

Form part:
Code:
// type
	echo ($error['type'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
	echo '<strong>Type</strong> (*)</td>';
	echo '<td><input type="radio" name="type[]" value="Replacement" ';
	if($_POST['type'][$x]=="Replacement") echo 'checked="checked"';
	echo ' />&nbsp;Replacement</td></tr>';
	echo '<tr><td> </td>';
	echo '<td><input type="radio" name="type[]" value="Enhancement" ';
	if($_POST['type'][$x]=="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>';

Validation part:
Code:
$error['type'][$key] = false; 
foreach($_POST['type'] as $key => $value) {
     if ($value=="") { 
        $error['type'][$key] = true; 
         $print_again = true;
    }
}

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

The page all loads properly and if i submit it all the validation will work (go red) apart from this bit. No matter what i do the "Type" text will never go red even if i don't fill it in? What should also happen is if the type is select as "Replacement" but nothing is entered into the asset field it should make the "Old assets?" text red...but none of thats happening!?
 
Oh and the radio buttons don't work too well as they're all named type[]! For example, the HTML form shows:

Code:
(item 1)
<input type="radio" name="type[]" value="Replacement" /> Replacement <br \>
<input type="radio" name="type[]" value="Enhancement" /> Enhancement.

(item 2)
<input type="radio" name="type[]" value="Replacement" /> Replacement <br \>
<input type="radio" name="type[]" value="Enhancement" /> Enhancement.

(item 3)
<input type="radio" name="type[]" value="Replacement" /> Replacement <br \>
<input type="radio" name="type[]" value="Enhancement" /> Enhancement.

and so on...

Is there a way around this?
 
Well that sorted it out so they're named different, but now it doesn't keep the value when the form is posted not completing validation, here's the code im using:

Code:
// 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][$x]=="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][$x]=="Enhancement") echo 'checked="checked"';
	echo ' />&nbsp;Enhancement';
	echo '</td></tr>';

I tried adding the $x after the POST bit but no luck there?
 
Code:
if($_POST['type'][$x][0] == '....')

use the print_r function to troubleshoot working with arrays eg

Code:
print_r($_POST);

that dumps the whole array structure and values on screen so you can see what you need to do.
 
Back
Top Bottom