SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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).

    Code:
    //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!

    Code:
    $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;
    	}
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there an example of how to do that?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Here's a better way. Do you see this code?

    Code php:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Here's a better way. Do you see this code?

    Code php:
    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.

    Code:
    $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 "<p>" . $quantity . " " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: " . $spice_level . "</p>";
    
    $_SESSION['total'] = $total;
    	}
    }
    and added the square boxes to the form as shown here...

    Code:
    //Rolls
    
    echo '<h3>Rolls</h3>';
    
    $query = mysql_query("SELECT * FROM `menu` WHERE category = 'rolls'");
    	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>';
    	}
    Still no luck though. I know the problem is probably in my process script correct? Thanks.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.

    Code php:
    foreach ($item as $index => $itemname)
    {
        // As $quantity and $spice_level are an array,
        // you should use $quantity[$index] to retrieve the appropriate value
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    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.

    Code php:
    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?

    Code:
    //Rolls
    
    echo '<h3>Rolls</h3>';
    
    $query = mysql_query("SELECT * FROM `menu` WHERE category = 'rolls'");
    	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>';
    	}
    Then I have the process script...

    Code:
    $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 => $itemname)
    {
    	
    $query = mysql_query("SELECT * FROM `menu` WHERE title = '$itemname'");
    	while($row = mysql_fetch_array($query)) {
    		
    		$total = $total + $row['price'];
    		
    echo "<p>" . $quantity[$index] . " - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: " . $spice_level[$index] . "</p>";
    
    $_SESSION['total'] = $total;
    	}
    }
    
    echo "<h3>ORDER TOTAL: $ $total</h3>";

    This is all posted at tandoorpgh.com/placeanorder

    I really appreciate your help with this.

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code php:
    $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:
    Code php:
    $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
    }
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •