Jump to content

Form checkbox values in PHP


mikebarbaro

Recommended Posts

Hello,

 

I have the following code displaying a checkbox with some other options as seen on www.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;
}
}

Link to comment
https://forums.phpfreaks.com/topic/190474-form-checkbox-values-in-php/
Share on other sites

Change your form code to

//Main Courses (non-vegetarian)

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

$query = mysql_query("SELECT * FROM `menu` WHERE category = 'entree'");
    $i = 0;
    while($row = mysql_fetch_array($query))
   {
      echo '<input type="checkbox" name="title[$i]" value="' . $row['title'] . '" /><b><span id=text_black>' . $row['title'] . '</span> - Quantity: <input type=text name="quantity[$i]" maxlength=1 size=1> - Spice Level: <select name="spice_level[$i]">
   <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>';
      $i++;
   }

 

When you process the form use

$item=$_POST['title'];
$_SESSION['items'] = $item;
foreach ($item as $itemkey => $itemname)
{
    $query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
   while($row = mysql_fetch_array($query)) {

      $total = $total + $row['price'];

      $_SESSION['quantity'] = $_POST['quantity'][$itemkey];

echo "<p>" . $_SESSION['quantity'] . " " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span></p>";

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.