Hi,
I have been working on an order form in PHP and trying to use arrays as the amount of items somebody could order may change. The problem's i currently have are mainly with some validation, if you go here:
http://richl.com/gpt/erf/erf_form.php
And fill out the form you'll notice that some values aren't validated properly. For example 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. Here is my 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 (is_numeric($value)) {
} 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:
<!-- 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 ' /> 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 ' /> 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>