PHP Form Checkbox Issue

Hello,

I have the following code displaying a checkbox with some other options as seen on tandoorpgh.com/placeanorder under the section Main Courses (non-vegetarian).

//Main Courses (non-vegetarian)

echo '<h3>Main Courses (non-vegetarian)</h3>';

$query = mysql_query("SELECT * FROM `menu` WHERE category = 'entree'");
	while($row = mysql_fetch_array($query)) {
		
		echo '<input type="checkbox" name="title[]" value="' . $row['title'] . '" /><b><span id=text_black>' . $row['title'] . '</span> - Quantity: <input type=text name=quantity maxlength=1 size=1> - Spice Level: <select name=spice_level>
	<option value=Mild>Mild</option>
    <option value=Medium>Medium</option>
    <option value=Hot>Hot</option>
    <option value=Extra Hot>Extra Hot</option>
</select></b>
		<span id="text_black">- $' . $row['price'] . '<br><i>' . $row['description'] . '</i></span>
		<br><br>';
	}

The problem I’m having is when the form process script runs, as seen below, the script only picks up the checkbox values which is the entrees’ name but I need it to attach the quantity and spice level to each item selected as well. Can anyone help? It’s appreciated!

$item=$_POST['title'];
$_SESSION['items'] = $item;
foreach ($item as $itemname)
{
	
$query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
	while($row = mysql_fetch_array($query)) {
		
		$total = $total + $row['price'];
		
		$_SESSION['quantity'] = $_POST['quantity'];
		
echo "<p>" . $_SESSION['quantity'] . " " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span></p>";

$_SESSION['total'] = $total;
	}
}

You will want to prefix the quantity and spice level with the title, so that they become unique named elements that you can retrieve later on during processing.

is there an example of how to do that?

Here’s a better way. Do you see this code?


echo '<input type="checkbox" name="title[]" value="' . $row['title'] . '" />

The square brackets allow an array of values to be passed through. Try using that on the name of the quantity, and the spice_level as well.

I added the following to my process script below.


$item = $_POST['title'];
$quantity = $_POST['quantity'];
$spice_level = $_POST['spice_level'];

$_SESSION['items'] = $item;
$_SESSION['quantity'] = $quantity;
$_SESSION['spice_level'] = $spice_level;

foreach ($item as $itemname)
{
	
$query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
	while($row = mysql_fetch_array($query)) {
		
		$total = $total + $row['price'];
		
echo "&lt;p&gt;" . $quantity . " " . $row['title'] . " - &lt;span style=color:#000;&gt;$" . $row['price'] . "&lt;/span&gt; - Spice Level: " . $spice_level . "&lt;/p&gt;";

$_SESSION['total'] = $total;
	}
}

and added the square boxes to the form as shown here…

//Rolls

echo '&lt;h3&gt;Rolls&lt;/h3&gt;';

$query = mysql_query("SELECT * FROM `menu` WHERE category = 'rolls'");
	while($row = mysql_fetch_array($query)) {
		
		echo '&lt;input type="checkbox" name="title[]" value="' . $row['title'] . '" /&gt;&lt;b&gt;&lt;span id=text_black&gt;' . $row['title'] . '&lt;/span&gt; - Quantity: &lt;input type=text name=quantity[] maxlength=1 size=1&gt; - Spice Level: &lt;select name=spice_level[]&gt;
	&lt;option value=Mild&gt;Mild&lt;/option&gt;
    &lt;option value=Medium&gt;Medium&lt;/option&gt;
    &lt;option value=Hot&gt;Hot&lt;/option&gt;
    &lt;option value=Extra Hot&gt;Extra Hot&lt;/option&gt;
&lt;/select&gt;&lt;/b&gt;
		&lt;span id="text_black"&gt;- $' . $row['price'] . '&lt;br&gt;&lt;i&gt;' . $row['description'] . '&lt;/i&gt;&lt;/span&gt;
		&lt;br&gt;&lt;br&gt;';
	}

Still no luck though. I know the problem is probably in my process script correct? Thanks.

When you loop through the items, you need a way to relate them to the quantity and the spice_level. You can use the index number of the item to do that.


foreach ($item as $index => $itemname)
{
    // As $quantity and $spice_level are an array,
    // you should use $quantity[$index] to retrieve the appropriate value

hmm it still just displays nothing for quantity or spice_level. Here’s my form script. Maybe it’s something wrong in there?

//Rolls

echo '&lt;h3&gt;Rolls&lt;/h3&gt;';

$query = mysql_query("SELECT * FROM `menu` WHERE category = 'rolls'");
	while($row = mysql_fetch_array($query)) {
		
		echo '&lt;input type="checkbox" name="title[]" value="' . $row['title'] . '" /&gt;&lt;b&gt;&lt;span id=text_black&gt;' . $row['title'] . '&lt;/span&gt; - Quantity: &lt;input type=text name="quantity[]" maxlength=1 size=1&gt; - Spice Level: &lt;select name="spice_level[]"&gt;
	&lt;option value=Mild&gt;Mild&lt;/option&gt;
    &lt;option value=Medium&gt;Medium&lt;/option&gt;
    &lt;option value=Hot&gt;Hot&lt;/option&gt;
    &lt;option value=Extra Hot&gt;Extra Hot&lt;/option&gt;
&lt;/select&gt;&lt;/b&gt;
		&lt;span id="text_black"&gt;- $' . $row['price'] . '&lt;br&gt;&lt;i&gt;' . $row['description'] . '&lt;/i&gt;&lt;/span&gt;
		&lt;br&gt;&lt;br&gt;';
	}

Then I have the process script…

$item = $_POST['title'];
$quantity = $_POST['quantity'];
$spice_level = $_POST['spice_level'];

$_SESSION['items'] = $item;
$_SESSION['quantity'] = $quantity;
$_SESSION['spice_level'] = $spice_level;

foreach ($item as $index =&gt; $itemname)
{
	
$query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
	while($row = mysql_fetch_array($query)) {
		
		$total = $total + $row['price'];
		
echo "&lt;p&gt;" . $quantity[$index] . " - " . $row['title'] . " - &lt;span style=color:#000;&gt;$" . $row['price'] . "&lt;/span&gt; - Spice Level: " . $spice_level[$index] . "&lt;/p&gt;";

$_SESSION['total'] = $total;
	}
}

echo "&lt;h3&gt;ORDER TOTAL: $ $total&lt;/h3&gt;";

This is all posted at tandoorpgh.com/placeanorder

I really appreciate your help with this.

In your particular case, the array does not work exactly fulfill your requirement. Because when you loop through the $items (titles) the loop will iterate only to those selected/checked checkboxes and it is difficult to relate other elements in the form. So as suggested above by Paul, you need to relate all the elements in the form (form in html page itself) with some unique ID or some index or something then in the same way you loop in the script something like this for example:

First of all prepare your form something like this:


$sql = "SELECT id, title, desc FROM tblname";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($result)):
	?>
	<input name="items[]" value="<?php echo $row->id; ?>" id="item_<?php echo $row->id; ?>" /> <?php echo $row->title; ?>
	Quantity: <input name="qty_<?php echo $row->id; ?>" id="qty_<?php echo $row->id; ?>" value="1" />
	<select id="spice_level_<?php echo $row->id; ?>" name="spice_level_<?php echo $row->id; ?>">
	</select>
	<?
endwhile;

And your script will be something like this:


$items = $_POST['items'];
foreach($items as $item){
	$qty = $_POST['qty_' . $item]; // now the quantity of that particular item will be stored here
	$spice_level = $_POST['spice_level_' . $item]; // now the quantity of that particular item will be stored here
}

Are the form inputs contained within a form?

Are the quantity and spice names related in some way to the title? An effective way is to use name=“quantity[‘.$id.’]”

Do you have a submit button to submit the form?

Do you check that $item isn’t empty, so that the foreach part is avoided when the page first loads?

Do you ensure that the POSTed information is an array?

Do you set a default value for $total so that it will have a meaningful value when you don’t go through the foreach part?

Do you protect the database from user-controlled bad data (purposeful or not) when you dump the POSTed into the database query?