The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Checkbox submissions
Discuss Checkbox submissions in the PHP Development forum on Dev Shed. Checkbox submissions PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 15th, 2013, 04:21 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
|
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++;
}
}
|

February 15th, 2013, 04:37 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Checkboxes are only submitted when they are checked.
|

February 15th, 2013, 04:40 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | 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.
|

February 15th, 2013, 04:44 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Yes it is the case. You're not understanding how your code works.
How are the quantities for the gin and Baileys?
|

February 15th, 2013, 04:59 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | 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.
|

February 15th, 2013, 05:09 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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.
|

February 15th, 2013, 05:23 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
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
Quote: | 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. |
|

February 15th, 2013, 05:34 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
|
I still don not understand why if I change the checkbox to a textbox the code functions as expected.
|

February 15th, 2013, 05:52 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Quote: | 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?
Quote: | 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.
|

February 15th, 2013, 05:57 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
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
Quote: | Originally Posted by requinix Relate what?
Because textboxes are always submitted, even if they're empty. |
|

February 15th, 2013, 06:30 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

February 15th, 2013, 06:41 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | 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++;
}
}
|

February 15th, 2013, 07:43 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
What is this $_POST['id'] coming from?
Actually you know what? What does your form look like?
|

February 16th, 2013, 08:55 AM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | 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>";
}
}
|

February 16th, 2013, 09:18 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 296
  
Time spent in forums: 3 Days 8 h 42 m 14 sec
Reputation Power: 5
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|