Jump to content

Having trouble with arrays in my PHP order form


echeats

Recommended Posts

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>

 

Link to comment
Share on other sites

Edit: sorry, have changed a little bit above:

 

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

To:

 

$error['theitem'][$key] = false; 
foreach($_POST['theitem'] as $key => $value) {
     if ($value=="n/a") { 
} else {
        $error['theitem'][$key] = true; 
         $print_again = true;
    }
}

Link to comment
Share on other sites

Ignore the above post, as 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 ' /> 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>';

Link to comment
Share on other sites

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

 

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?

Link to comment
Share on other sites

on first glance i believe the problem is the way you set your form vars:

 

try using "thetype[]" instead of "thetype[0][]".

 

then check validation like so:

 

if(@$_POST['thetype'][0] == "Enhancement"){
     // enhancement code
}elseif((@$_POST['thetype'][0] == "Replacement"){
     // Replacement Code
}else{
      // Field not Chosen, error stuff here
}

if(@$_POST['thetype'][1] == "Enhancement"){
     // enhancement 2 code
}elseif((@$_POST['thetype'][1] == "Replacement"){
     // Replacement 2 Code
}else{
      // Field 2 not Chosen, error 2 stuff here
}

 

 

im assuming radio post vars give the value just like other form variables

hope this helps,

Link to comment
Share on other sites

I have several groups of radio inputs, for example:

 

<td><strong>Type</strong> (*)</td>
<td><input type="radio" name="thetype[0][]" value="Replacement"  /> Replacement</td></tr>
<tr><td> </td>
<td><input type="radio" name="thetype[0][]" value="Enhancement"  /> Enhancement</td></tr>

 

And this repeats four or five times (so giving thetype[1][], thetype[2][], etc) and without these giving them invididual names it means if i choose either radio option from anywhere on the page it will change it as they all have the same name (if you see what i mean!)

 

This is why i think i just need to change the validation to accept the [(number)] value:

 

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

Link to comment
Share on other sites

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

 

Can anyone help?

Link to comment
Share on other sites

You need to use return; for it not to continue. Here is what I did created for one of my forms:

 

if(isset($_POST['submit'])) {

$name = $_POST['name'];
if ($name =="")
{
echo 'Please fill this out';
return;
}
$comment = $_POST['comment'];
if ($comment =="")
{
echo 'please fill this 2 out';
return;
}

 

It basically checks if the field is blank, if it is, it returns an error.

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.