...

View Full Version : Having trouble with arrays in my PHP order form



echeats
03-08-2008, 12:39 PM
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.

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!#$&#37;&'*+/=?^_`{|}~-][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'];


// define variables


// 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 \>';
}

}


}
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 ' />&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>

abduraooft
03-08-2008, 12:46 PM
$error['theitem'][$key] = false;
foreach($_POST['theitem'] as $key => $value) {
if (is_numeric($value)) {
} else {
$error['theitem'][$key] = true;
$print_again = true;
}
}

<select name="theitem[]"><option>Brother HL-5240</option><option value="n/a">-</option>
<option>Brother HL-5240</option>
<option>Brother HL-5240 with Lower Tray</option>
<option>Brother LT-5300 Lower Tray (for 5240)</option>
<option>Network Cabling Run</option>
<option>Desktop PC and TFT</option>
<option>Desktop PC (no TFT)</option>
<option>Standard TFT Screen</option>
<option>HP Colour Laserjet 5400</option>
<option>24port Switch</option>
<option>Other</option></select>
You have to set value attribute for all your options.

echeats
03-08-2008, 12:52 PM
ignore

echeats
03-08-2008, 12:58 PM
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:

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

In erf_html.php:

// 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>';

abduraooft
03-08-2008, 01:24 PM
<input type="radio" value="Replacement" name="thetype0[]"/>
<input type="radio" value="Enhancement" name="thetype[0][]"/>
They won't behave as a group. both of them needs the same name!

echeats
03-08-2008, 04:31 PM
Ok have sorted that bit but it's still not working, i think there is something wrong with the validation bit of code shown here:


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

As the form is coded like this:


// 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>';

I think i need to put the $x on the end of the POST_('thetype') bit of validation code but am not sure how to?

abduraooft
03-08-2008, 04:46 PM
I believe, you don't need the thetype[][] 2D array in the html since only one value will be posted from a radio group. thetype[0] and thetype[1] would be enough.

echeats
03-08-2008, 04:55 PM
When i didn't have the other part the form couldnt differentiate between the radio button sections, so if i repeated the form twice and select "Replacement" from the first form and "Enhancement" from the second, it would switch my first option over to the second form (if you see what i mean!). That was why i had to include the $x into the radio name:



name="thetype[' . $x . '][]"


so they would then all have a unique name per form. Only problem is i now don't know how to put that into my validation? If there's an easier/better way of doing it id appreciate the help :)

echeats
03-08-2008, 06:17 PM
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:

Validation:

$error['thetype'][$key] = false;
foreach($_POST['thetype'][$x][0] as $key => $value) {
if (strlen($value < 5)) {
$error['thetype'][$key] = true;
$print_again = true;
}
}

Form:

// 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>';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum