• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

HTML/PHP: input type hidden, PHP shopping cart

My project is for a pizza shop online ordering system. I want to display all of a category of item, such as salads, breads or desserts, on one screen. My products page has a table of the items. Each row is a record of one item with a cell for each of these fields:

      item name
      item description
      item cost
      blank text field for user to enter quantity
      AddToCart button


Ideally I want the user to enter the quantity & then click the AddToCart button & add that quantity into the cart. I can't get that to work. Since I can't pick up the quantity value, I tried to get one item into the cart. That doesn't work either. I've tired many variations of the code below & I think my problem is in the initial loading of the html code. When I'm running the app & step through it, menu_items, in the HTML code below, isn't getting set.

The code below is the code that builds the relevant part of the menu table.

<?php      
    $query = "SELECT * FROM menuitems WHERE (CategoryID = $category_id) && (Active = True)";
    $statement = $db->prepare($query);
    $statement->execute();
    $extras = $statement->fetch();      //get the first row
    //echo '<input type="hidden" name="action" value="menu_items[]" />';    
    while  ($extras !=null) {
        echo '<tr id="extras">';
        echo '<td class="cellLeft" style="width: 175px; border-bottom: black thin solid;" align="left"">';         
        echo $extras['Name'];
        echo '</td>';

        echo '<td class="cellLeft" style="width: 350px; border-bottom: black thin solid;" align="left" valign="top">';
        echo $extras['Description'];
        echo '</td>';

        echo '<td class="cellRight" style="width: 65px; border-bottom: black thin solid;" align="right" valign="top">';
        echo $extras['Price'];
        echo '</td>';

        echo '<td class="cellRight" style="width: 65px; border-bottom: black thin solid;" align="center" valign="center">';        
        echo '<input type="text" class="cart_qty" maxlength="2" name="qty" value="0" />';
        echo '</td>';
        
        echo '<td style="width: 65px; border-bottom: black thin solid;" align="center" valign="center">';  
        echo '<form action="." method="post">';
        echo '<input type="hidden" name="action" value="menu_item" />';
        echo '<input type="hidden" name="menu_item" value="';
        echo $extras['Value'];
        echo '" />';
        echo '<a href=".?action=add_extras" value="submit" target="_self"><img src="images/btnPlus-red.png" alt="addToCart" width="25" height="25" border="0" /></a>';        
        echo '</form></td>';        
        echo '</tr>';
        $extras = $statement->fetch();      //get the next row
    }
    $statement->closeCursor();
    echo '</table>';
?>

Open in new window


This code is the index file where, in step mode, you can see that menu_items isn't set.

case 'add_extras':
        if (isset($_POST['menu_item'])) {
            //add to cart
            $x = count('menu_item');
        }
        break;

Open in new window


I think the problem is somewhere in my form action or hidden fields. ???

Thanks for any & all suggestions!  
0
LezlyPrime
Asked:
LezlyPrime
  • 2
1 Solution
 
Scott MadeiraCommented:
First of all, your qty box needs to be inside the <form>    </form> structure if you want it to be sent to the server when the button is pushed.  Try what is shown below.  you will need to play with the CSS to get the layout correct.

 
while  ($extras !=null) {
        echo '<tr id="extras">';
        echo '<td class="cellLeft" style="width: 175px; border-bottom: black thin solid;" align="left"">';         
        echo $extras['Name'];
        echo '</td>';

        echo '<td class="cellLeft" style="width: 350px; border-bottom: black thin solid;" align="left" valign="top">';
        echo $extras['Description'];
        echo '</td>';

        echo '<td class="cellRight" style="width: 65px; border-bottom: black thin solid;" align="right" valign="top">';
        echo $extras['Price'];
        echo '</td>';

        echo '<td style="width: 65px; border-bottom: black thin solid;" align="center" valign="center">';  
        echo '<form action="." method="post">';
        echo '<input type="hidden" name="action" value="menu_item" />';
        echo '<input type="hidden" name="menu_item" value="';
        echo $extras['Value'];
        echo '<input type="text" class="cart_qty" maxlength="2" name="qty" value="0" />';
        echo '" />';
        echo '<a href=".?action=add_extras" value="submit" target="_self"><img src="images/btnPlus-red.png" alt="addToCart" width="25" height="25" border="0" /></a>';        
        echo '</form></td>';        
        echo '</tr>';
        $extras = $statement->fetch();      //get the next row
    }

Open in new window

0
 
Scott MadeiraCommented:
Also, to help figure out the problem, add the following code to your index file to see what exactly is in the $_POST array you are getting from the form.

echo '<pre>';
print_r($_POST);
echo '</pre>';

Also,you need to look at how to build a submit button from an image.  It isn't a <a> tag.  You need to define an input with the type "image" as part of the form.  You can start here and look at an input type of "image"  http://www.w3schools.com/tags/att_input_type.asp


0
 
Hugh McCurdyCommented:
I would add this command to the PHP code

var_dump ( $_POST );

Also, is this a class project or an e-commerce project?  If it's an e-commerce project, you have security problems.
0
 
LezlyPrimeAuthor Commented:
Thank you. I got it to work based on your input. I have a better understanding of hidden fields, now. Also, thanks for your image tip below.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now