Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73

    Checkbox submissions


    Let me start by saying yes I know there are vulnerabilities in the code. What I do not understand is why the check boxes are not working in the same manner as an input text does. (the input text works fine.
    What is happening is if i select three options in the middle of the form the first three records are posting. If I select two then the first two post. so on and so on



    Form:
    PHP Code:
    echo "<input type=checkbox name=pulledQty[] value=$inverseQty>";

    outputs something like this:

    Bacardi Rum            31   "checkbox"
    Baileys Irish Cream    10   "checkbox"
    Bombay Gin                    10   "checkbox"
    Bottled Water            10   "checkbox"
    Captain Morgan Rum    10   "checkbox"
    Chardonnay                10   "checkbox"
    Club Soda                    10   "checkbox" 

    Input page:
    PHP Code:
    if(isset($_POST['pulledItem']))
    {
       
    $pulledItem $_POST['pulledItem'];
       
    $pulledQty $_POST['pulledQty'];
       
    $n        count($pulledItem);
       
    $i        0;
       while (
    $i $n)
       {
    $dbh=mysql_connect ($data_base_connection$user$password) or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("hmsglobal");
    $result=mysql_query("INSERT INTO unionInv (unionInvItem,unionQty) VALUES ('{$pulledItem[$i]}','{$pulledQty[$i]}')")or die("Insert Error: ".mysql_error());
    mysql_close;
    $date addslashes($date);
          
    $i++;
       }

  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    Checkboxes are only submitted when they are checked.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    Checkboxes are only submitted when they are checked.
    Yep that is what I thought also but for some reason in this case that is not the case. If I select Chardonnay and Club Soda. Bacardi Rum and Baileys Irish Cream will post. If I select Bombay Gin and Bottled Water. Bacardi Rum and Baileys Irish Cream will post.
    For testing I changed the check boxes to text fields and everything behaves as expected.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    Yes it is the case. You're not understanding how your code works.

    How are the quantities for the gin and Baileys?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    Yes it is the case. You're not understanding how your code works.

    How are the quantities for the gin and Baileys?
    Sorry, I dont quite understand what you are asking me. Is this what you are asking?

    PHP Code:
    $inverseQty $row['exchOrder']*-1;
    echo 
    "<input type=checkbox name=pulledQty[] value=$inverseQty>"
    even if i take everything away and just have the following it is still behaving in the same manner?

    echo "<input type=checkbox name=pulledQty[] value=10>";


    this is the entire form:
    PHP Code:
    $result mysql_query("SELECT inventory.itemID,inventory.id,exchMinOrder.exchMin,exchMinOrder.exchOrder,exchInvItemID,SUM(exchQty) FROM inventory LEFT JOIN exchMinOrder ON inventory.id = exchMinOrder.exchItemID LEFT JOIN exchInv ON exchInv.exchInvItemID = inventory.id GROUP BY inventory.itemID"
    or die(
    mysql_error());  

    echo 
    "<div class=masterSubBox>";

    echo 
    "<center>";
    echo 
    "<table width=80% border=0 cellpadding=10 cellspacing=0>";
        echo 
    "<tr>";
            echo 
    "<td>";
                echo 
    "Item ID";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                echo 
    "Qty to Restock";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                echo 
    "All Qty Pulled";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                echo 
    "Other Qty";
            echo 
    "</td>";
        echo 
    "</tr>";
    while(
    $row mysql_fetch_array$result )) {
                                        


    echo 
    "<input type=hidden name=locFrom value=$locFrom>";
        echo 
    "<tr>";
            echo 
    "<td>";
                echo 
    $row['itemID'];
                echo 
    "<input type=text name=pulledItem[] value=".$row['id'].">";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                echo 
    $row['exchOrder'];
            echo 
    "</td>";
            echo 
    "<td align=center>";
            echo 
    $row['id'];
                        
                echo 
    "<input type=checkbox name=pulledQty[] value=10>";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                    echo
    "<input type=text name=disQty[] size=5>";

            echo 
    "</td>";
        echo 
    "</tr>";
            
    }
    echo 
    "</table>"
    Last edited by jlewis01; February 15th, 2013 at 05:08 PM.
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    I mean the data being saved. If I understand how your form works, the quantities saved for the gin and Baileys will be both 10. Or -10? Not 31 and 10 like I think you showed.

    With two checkboxes PHP will receive something like
    Code:
    pulledQty[]=10&pulledQty[]=10
    With three,
    Code:
    pulledQty[]=10&pulledQty[]=10&pulledQty[]=10
    Either way $_POST["pulledQty"] will contain an array of 2 or 3 items, and there's nothing in there that indicates whether the checkboxes correspond to items 0, 1, 2 or 0, 3, 19. The code just pulls them out in order and so they'll be paired up with the first three items it finds (which, unlike the checkboxes, were all submitted).

    Couple options:

    The way I prefer to do this is to find a way to relate the checkboxes to the items they pair up with. Typically that's with an array key, like
    Code:
    <input type="checkbox" name="pulledQty[Bacardi Rum]" value="31" />
    (though it's generally an ID number and not an actual name)
    Your loop over the items can then (1) look to see if there's a related quantity in the pulledQty array and (2) use that value if present or do something else (eg, use 0 or skip the item entirely) if not.

    The way I do not like still uses array keys but doesn't need to actually relate the checkbox to the item. It forces there to be a value every time, regardless if the checkbox is checked.
    Code:
    <input type="hidden" name="pulledQty[0]" value="0" />
    <input type="checkbox" name="pulledQty[0]" value="31" />
    The hidden input will always be sent thus the default is the value 0. If the user hits the checkbox then its value will overwrite the default and you'll get 31 instead.

    [edit] Note that you still need array keys for this: with just "pulledQty[]" you'll only append new values to the array - not overwrite.
    Last edited by requinix; February 15th, 2013 at 05:12 PM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Okay, I understand using the key, I can use the value from ID. What I dont get is how to then relate that to submitting it

    Originally Posted by requinix
    I mean the data being saved. If I understand how your form works, the quantities saved for the gin and Baileys will be both 10. Or -10? Not 31 and 10 like I think you showed.

    With two checkboxes PHP will receive something like
    Code:
    pulledQty[]=10&pulledQty[]=10
    With three,
    Code:
    pulledQty[]=10&pulledQty[]=10&pulledQty[]=10
    Either way $_POST["pulledQty"] will contain an array of 2 or 3 items, and there's nothing in there that indicates whether the checkboxes correspond to items 0, 1, 2 or 0, 3, 19. The code just pulls them out in order and so they'll be paired up with the first three items it finds (which, unlike the checkboxes, were all submitted).

    Couple options:

    The way I prefer to do this is to find a way to relate the checkboxes to the items they pair up with. Typically that's with an array key, like
    Code:
    <input type="checkbox" name="pulledQty[Bacardi Rum]" value="31" />
    (though it's generally an ID number and not an actual name)
    Your loop over the items can then (1) look to see if there's a related quantity in the pulledQty array and (2) use that value if present or do something else (eg, use 0 or skip the item entirely) if not.

    The way I do not like still uses array keys but doesn't need to actually relate the checkbox to the item. It forces there to be a value every time, regardless if the checkbox is checked.
    Code:
    <input type="hidden" name="pulledQty[0]" value="0" />
    <input type="checkbox" name="pulledQty[0]" value="31" />
    The hidden input will always be sent thus the default is the value 0. If the user hits the checkbox then its value will overwrite the default and you'll get 31 instead.

    [edit] Note that you still need array keys for this: with just "pulledQty[]" you'll only append new values to the array - not overwrite.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    I still don not understand why if I change the checkbox to a textbox the code functions as expected.
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    Originally Posted by jlewis01
    Okay, I understand using the key, I can use the value from ID. What I dont get is how to then relate that to submitting it
    Relate what?

    Originally Posted by jlewis01
    I still don not understand why if I change the checkbox to a textbox the code functions as expected.
    Because textboxes are always submitted, even if they're empty.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    I do not know how to relate that to the submission page. I know that "<input type=checkbox name=pulledQty[$id] value=10> assigns a key value to the checkbox but I dont know how then to have the information posted so it can be inserted into the database

    Originally Posted by requinix
    Relate what?


    Because textboxes are always submitted, even if they're empty.
  20. #11
  21. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    If what's in pulledItem are those $id values you're using then sure.

    Right now you have a for loop. In code it has a "while" but really it's a for loop: $i counts from 0 up to $n. Inside it grabs the quantity from $pulledQty according to $i. That $i is not the right key anymore - you have to use the value from the $pulledItem instead.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    If what's in pulledItem are those $id values you're using then sure.

    Right now you have a for loop. In code it has a "while" but really it's a for loop: $i counts from 0 up to $n. Inside it grabs the quantity from $pulledQty according to $i. That $i is not the right key anymore - you have to use the value from the $pulledItem instead.
    so are you saying something like this?

    if(isset($_POST['pulledItem']))
    {
    $id = $_POST['id'];
    $pulledItem = $_POST['pulledItem'];
    $pulledQty = $_POST['pulledQty'];
    $n = count($pulledItem);
    $id = 0;
    while ($id < $n)
    {
    $dbh=mysql_connect ($data_base_connection, $user, $password) or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("hmsglobal");
    $result=mysql_query("INSERT INTO unionInv (unionInvItem,unionQty) VALUES ('{$pulledItem[$id]}','{$pulledQty[$id]}')")or die("Insert Error: ".mysql_error());
    mysql_close;
    $date = addslashes($date);
    $id++;
    }
    }
  24. #13
  25. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    What is this $_POST['id'] coming from?

    Actually you know what? What does your form look like?
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    What is this $_POST['id'] coming from?

    Id is the primary key value assigned to the part numbers. I included it when you said a key needed to be assigned to values so their relation could be defined.


    Actually you know what? What does your form look like?
    This is what my form currently looks like. Honestly, I understand why to include a key to the array but I am clueless when it comes to apply that understanding.
    PHP Code:
    while($row mysql_fetch_array$result )) {
                                        
        
        if (
    $row['SUM(exchQty)'] < $row['exchMin'] || $row['SUM(exchQty)'] == $row['exchMin']) {
            



    echo 
    "<input type=hidden name=locFrom value=$locFrom>";
        echo 
    "<tr>";
            echo 
    "<td>";
                
    $id $row['id'];
                
                echo 
    $row['itemID'];
                        
                echo 
    "<input type=text name=pulledItem[$id] value=".$row['id'].">";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                echo 
    $row['exchOrder'];
            echo 
    "</td>";
            echo 
    "<td align=center>";

                        echo 
    "<input type=text name=id[$id] value=".$row['id'].">";
                echo 
    "<input type=checkbox name=pulledQty[$id] value=10>";
            echo 
    "</td>";
            echo 
    "<td align=center>";
                    echo
    "<input type=text name=disQty[] size=5>";

            echo 
    "</td>";
        echo 
    "</tr>";
            }

  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    I'd say try focusing your mind on 2 items. As far as when inserting into your database, what will unionInvItem,unionQty be? One a name, the other a number? If unionInvItem is a number, then the 2 items we've in mind is itemID and itemQty. For your key portion of the checkbox, make it your itemID, and the 2nd part will hold itemQty. Then, on your page to insert into database, you can do shorthand if/then to assign the submitted values to the variables being inserted.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo