0

The title is probably confusing, but here is what I am trying to do and failing miserably at:

I want to display all the items from my db table named 'inventory'.

Then, a user would select one item by clicking a 'Add to cart' image button.

When the button is clicked, the form is submitted to "addtocart.php" for processing the item selected and gets directed back to the inventory page.

Now that you know what i'm trying to do, here is where I am stuck:

I have all the items displayed in a table.

The query is "Select * from pantry", and then I display it like this:

while($pantry = mysql_fetch_assoc($sql))
{
echo "<tr><td>" . $pantry . "</td><td>" . $pantry . "<input type=hidden name=item value='".$pantry."'><input type=image class=addcart></td></tr>";
}

so far, so good, it displays each item and the add to cart submit button.


The problem is, when it gets processed by "addtocart.php", it only recognizes the very last item displayed. What I mean is, if I add item A to my cart and hit submit, "addtocart.php" will add item Z to the database. Item Z is the very last item that is displayed on the page, and is what gets inserted to the db no matter what item I actually selected.

I guess the problem is in my while loop some where. The purpose of the hidden input is supposed to be to assign the item's name to the form variable, but as I said, each item is not getting assigned their own unique name, they are all assigned "item Z"!

What am I doing wrong?

3
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by flynismo
0

It looks like you're setting every item to have the same name: "name=item"

You need to make each item have its own unique name (I usually use the item ID #)
You currently have all items with the same name, making the POST use only the last one.

0

Okay, that confused me at first but it makes sense when I think about it...If I understand you correctly, I should delete the value= part of input, and use that as the name instead. (i.e. -- name='".$pantry"')

Just for the sake of learning, I was curious about something:
when I was trying to troubleshoot this last night, I changed the input type from text to a checkbox, and it worked fine, even though the input name instead was still the same for each. Why is that? Do forms/php handle checkboxes differently somehow?

0

Hidden inputs will always send the value AFAIK. Checkboxes only send values when they are checked.

I really can't see what you're trying to do with the hidden field and the image field. The image button will send the entire form won't it (every field)?

0

Hidden inputs will always send the value AFAIK. Checkboxes only send values when they are checked.

I really can't see what you're trying to do with the hidden field and the image field. The image button will send the entire form won't it (every field)?

I'm glad you point that out about the image field entering the entire form -- i didnt even realize that was happening until i changed the form method from POST to GET to see if that would give me any clues.

The idea was supposed to be that the image button would display next to each item, and when clicked, that particular item would be added to the cart. But that isnt working, because as you said, the entire form is getting sent.

The problem is technically solved - I am just going to stick with the checkbox, and use a normal submit button, because that seems to be more user friendly anyway (can submit multiple items at once without reloading the page every time they add an item)..but still would like to figure out how to do it with image buttons just in case I ever would be required to do that.

Thanks for the input guys!

0

For the image thing, if the image is your button, instead of using POST, just link the picture to a page with a URL var that points to your item:

echo '<a href="cart.php?additem="'.$pantry['id'].'">Some Time info</a>';

so if the item id was 2: cart.php?additem=2
then just use $_GET to find the id and add that item to the user's cart

Edited by makman99: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.