View Full Version : Getting Postback values from Dynamic Checkboxes

01-27-2010, 04:05 PM

I am trying to make an online sandwich order form. So I have different lunch meats, bread, condiments and so on in mysql database. Everything I created is drop down box, however I have the condiments listed as checkboxes that are dynamically driven by another table. Here is the code for the checkboxes. When the user hits submit it shows on the users screen what they ordered for a sandwich and sends an email to our deli. Everything works except the condiments - as I do not know how to capture the value into a php variable. Should I use an array to do this sense the number of condiments could increase or decrease? Here is the code for selecting the condiments.
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<td align="center" valign="top"><strong>Condiments</strong><br>
<table width="399" cellpadding="0" cellspacing="0">
<?php do { ?>
<td width="180" align="right" valign="middle" bgcolor="#FFFFFF"><?php echo $row_condiment['condiment']; ?>
<input name="c1" type="checkbox" id="c5" value="<?php echo $row_condiment['condiment']; ?>" /></td>
<td width="20" align="left">&nbsp;</td>
<?php $row_condiment = mysql_fetch_assoc($condiment) ?>
<td width="180" align="left" valign="middle" bgcolor="#FFFFFF"><input name="c2" type="checkbox" id="c4" value="<?php echo $row_condiment['condiment']; ?>" />
<?php echo $row_condiment['condiment']; ?></td>
<?php } while ($row_condiment = mysql_fetch_assoc($condiment)); ?>
Hope this makes sense - sorry I am a newb still haha.

Thank you,

01-27-2010, 05:24 PM
Your checkbox name should be like this: name="cond[]"
That creates an array for all of the checkboxes.

<input name="cond[]" type="checkbox" id="c5" value="<?php echo $row_condiment['condiment']; ?>" /></td>

When it's posted to the script that processes the form,
you will then loop through and read that array. Only the
items that were checked will exist, and the value of the
array element will be the value of the checkbox,
example, $condiments[1] might be "Onions".

To read your checkbox array ... would be similar to this ...

// read the checkbox array
while (list ($key,$val) = @each ($condiments)) {
// do something with the value here.
// maybe create another array to use later on?
// for now, just echo to see what was selected.
echo $val."<br>";

01-27-2010, 05:38 PM
Wow! Awesome Thank you so much!

02-18-2010, 03:56 PM
That worked awesome!!!

Everytime I think I start to learn this php thing I run into another issue. :mad:

Kind of an offtopic question but sort of similiar...

I am creating an order form with dynamic rows..
so I have

Bread Type | Price |Quantity ordered

Price and quantity ordered are textboxes. So if I name Price - "price[]" and "quantity[]" will I be able to take individual values? What I am really trying to do is post back the Quantity ordered for each row - and also multiply each rows price by quantity ordered and add all of the rows to get a total

Anyone know of an example tutorial / code similiar to this I can look at?

02-18-2010, 04:08 PM
I dont know if I explained myself very well..

basically I am just wondering how I can name ids in a dynamic table.

so in row 1 I want textbox1 id = textbox1 and in row 2 I want textbox2 id = textbox2 and so on so I can use these values

02-18-2010, 04:22 PM
hmmm nvm its not pretty but I think I found a solution to my question.