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?!
 
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;
    }
}
 
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:
 
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)
 
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?!
 
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?
 
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?
 
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>';
 
Back
Top Bottom