Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Having trouble with arrays in my PHP order form

    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:

    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:


    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 by echeats; 03-10-2008 at 11:56 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    $error['theitem'][$key] = false;
    foreach($_POST['theitem'] as $key => $value) {
    if (is_numeric($value)) {
    } else {
    $error['theitem'][$key] = true;
    $print_again = true;
    }
    }
    Code:
    <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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    echeats (03-08-2008)

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ignore
    Last edited by echeats; 03-08-2008 at 11:59 AM.

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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>';

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    <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!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    echeats (03-08-2008)

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    Code:
    $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:

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

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:

    Code:
    	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

  • #9
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:
    Code:
    $error['thetype'][$key] = false; 
    foreach($_POST['thetype'][$x][0] as $key => $value) {
         if (strlen($value < 5)) {
            $error['thetype'][$key] = true; 
             $print_again = true;
        }
    }
    Form:
    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>';


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •