Help with form vars not posting
Results 1 to 2 of 2

Thread: Help with form vars not posting

  1. #1
    Junior Member
    Join Date
    Apr 2012

    Help with form vars not posting

    Hello there, Im currently working on a system that holds invoices and creates them aswell as other things.

    Im stumped on something i need help with. I have a area where the invoice that is created needs editing. At the side of the part lines i have a check box that has a check box and when ticked will delete that line once the edited invoice is submitted. Everything else works on the form, it will add new lines and edit lines. but when i click one of the tick boxes and submit it will only remove line 1 of the parts.

    I have gone over and over the code and it all looks ok. The data is stored in mysql database

    My code

    Template file - {parts_list} is replaced with what php works out and shown to user
    HTML Code:
    <table id="mytab" width="100%">
             <th width="166"><a href="#">Part No</a></th>
             <th width="402" align="center"><a href="#">Description</a></th>
             <th width="71" align="center"><a href="#">Qty</a></th>
             <th width="162" align="center"><a href="#">Supplier</a></th>
             <th width="134"><a href="#">S. Inv No</a></th>
             <th width="127"><a href="#">S. Net</a></th>
             <th width="146"><a href="#">Net</a></th>
             <th width="5"><a href="#">x</a></th>
       <p align="center"> <input type="button" value="Add a new part line" onclick="addRow()"></p>
    edit_invoice.php - part of file that works out what the part list is and gen html to insert into the template {parts_list} var
    PHP Code:
     //get parts list
    $query_parts mysql_query("SELECT * FROM invoice_parts WHERE `int_inv_no`='".$result1["inv_no"]."'");
    $row mysql_fetch_array($query_parts)) {
    $supplier_list .= ' <select name="supplier[]" id="supplier[]">
          <option value="0" selected="selected">---------------</option>'
    $mysql_supplier_query mysql_query("SELECT * FROM suppliers");
    $row2 mysql_fetch_array($mysql_supplier_query)) {
    $row["sid"] == $row2["sid"]) {
    $supplier_list .= '<option value="'.$row2["sid"].'" selected="selected">'.$row2["name"].'</option>';
                  } else {
    $supplier_list .= '<option value="'.$row2["sid"].'">'.$row2["name"].'</option>';
    $supplier_list .= '</select>';
    $parts_list .= '<tr>
                    <td><input name="part_no[]" type="text" id="part_no[]" size="5" value="'
    .$row["part_no"].'" /><input name="pid[]" type="hidden" id="pid[]" value="'.$row["id"].'" /></td>
                      <input name="desc[]" type="text" id="desc[]" size="20" value="'
    .$row["desc"].'" />
                    <td><input name="qty[]" type="text" id="qty[]" size="1" value="'
    .$row["qty"].'" /></td>
                    <td><input name="s_inv[]" type="text" id="s_inv[]" size="10" value="'
    .$row["sup_inv_no"].'" /></td>
                    <td><input name="s_net[]" type="text" id="s_net[]" size="2" value="'
    .$row["sup_net_price"].'" /></td>
                    <td><input name="net[]" type="text" id="net[]" size="2" value="'
    .$row["ur_net_price"].'" /></td>
                   <td><input name="delete[]" type="checkbox" id="delete[]" value="yes" /></td>
    //end while loop
    $parts_list .= '<tr>
                    <td><input name="part_no[]" type="text" id="part_no[]" size="5" /></td>
                      <input name="desc[]" type="text" id="desc[]" size="20" />
                    <td><input name="qty[]" type="text" id="qty[]" size="1" /></td>
                    <td><input name="s_inv[]" type="text" id="s_inv[]" size="10" /></td>
                    <td><input name="s_net[]" type="text" id="s_net[]" size="2" /></td>
                    <td><input name="net[]" type="text" id="net[]" size="2" /></td>
                    <td><input name="delete[]" type="checkbox" id="delete[]" value="yes" /></td>

    submit_edit_invoice.php - Takes all info from form from edit_invoice.php and updates the database - This is the bit that deals with the editing and removing of parts
    PHP Code:
    foreach ($_POST["part_no"] as $key => $value) {
    //post vars
    $pid $_POST["pid"][$key];
    $part_no $_POST["part_no"][$key];
    $desc $_POST["desc"][$key];
    $qty $_POST["qty"][$key];
    $supplier $_POST["supplier"][$key];
    $s_inv $_POST["s_inv"][$key];
    $s_net $_POST["s_net"][$key];
    $net $_POST["net"][$key];
                            if (
    $pid "0") { // is already in db because it has a pid number
    if(isset($_POST["delete"][$key]) && ($_POST["delete"][$key] == "yes")) {
    $queryremove_part mysql_query("DELETE FROM invoice_parts WHERE `id`='".$_POST["pid"][$key]."'");
    $queryremove_part) { echo '<p>Error performing query remove invoice parts: ' mysql_error() . '</p>'; }
                                    } else {
    Every other part of the edit invoice works just the removing of one or more invoice lines. If i do select a line to remove it always does the top line, or i assume its the last line called from the database when its pulled.

    Sorry about the long post, iv included everything i can think off so anyone helping has everything to hand


  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    General Contact Unit "Coping Mechanism"
    The value of an id attribute in an HTML page must be unique.

    Also, you have no way of identifying which checkboxes were checked (they all have the same name, the same value, and only checked checkboxes are submitted, so $_POST['delete'] will just end up being an array('yes', 'yes', 'yes', ...) one 'yes' for each checked checkbox.

    For both reasons, I suggest using explicit keys in your form field names - the id is an obvious choice, and would also mean the hidden field is not necessary:

    PHP Code:
    td><input name="part_no['.$row['id'].']" type="text" size="5" value="'.$row["part_no"].'" /></td>
    input name="desc['.$row['id'].']" type="text" size="20" value="'.$row["desc"].'" /> 
    Now you know which form fields belong to which part - including the checkboxes.
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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