Jump to content

adding items to a shopping cart through html form


pwesthead

Recommended Posts

hi 

i have a shopping cart where i get the products from mysq,l the problem is whatever item i add to my cart it only add the last entry from the database here is my code

 

<form method="post" action="" class="jcart">
<fieldset>
 <input type="hidden" name="my-item-id" value="<?php echo $all['id'];?>" />
<input type="hidden" name="my-item-name" value="<?php echo $all['title'];?>" />
<input type="hidden" name="my-item-price" value="<?php echo $all['sale_price'];?>" />

                        <input type="hidden" name="my-item-qty" value="1" size="3" />
<!--product item-->
<?php
    //retrive categories from the categories table

    $allitems=mysql_query("SELECT * FROM dg_products  ")
    or die("There are no records to display ... \n" . mysql_error()); 
?>
        
       
  <?php  while ($all=mysql_fetch_array($allitems))
    {


?> 


<div class="product_item">
<figure class="r_corners photoframe shadow relative hit animate_ftb long">

                 <!--product preview-->
<a href="#" class="d_block relative pp_wrap">
<!--hot product-->
<span class="hot_stripe"><img src="images/hot_product.png" alt=""></span>
<img src="/keiths/<?php echo $all['image'];?>" class="tr_all_hover" alt="">
<span data-popup="#<?php echo $all['id']?>" class="button_type_5 box_s_none color_light r_corners tr_all_hover d_xs_none">Quick View</span>
</a>
<!--description and price of product-->
<figcaption>
<h5 class="m_bottom_10"><a href="#" class="color_dark"><?php echo $all['title'];?> </a></h5>
<div class="clearfix">
<p class="scheme_color f_left f_size_large m_bottom_15"><s>&pound<?php echo $all['price'];?></s> &pound<?php echo $all['sale_price'];?></p>

                                   <input type="submit"class="button_type_4 bg_scheme_color r_corners tr_all_hover color_light f_left mw_0" name="my-add-button" value="add to cart" /></form>

 

Link to comment
Share on other sites

you are outputting all the product information inside of one <form></form>, so only the values from the last set of same-name form fields 'wins'.

 

wouldn't you want to output the information for each product in it's own form, so that when you submit any one form, the only thing that gets submitted is the data for the product that you want?

Link to comment
Share on other sites

No offense, but this is a hot mess. As benanamen points out, you're using code that is about a decade out of date for dealing with the database calls. Not only that, you're attempting to use the $all variable before it's defined and you're not associating any of the 'add to cart' buttons to a specific product so how is your processing script supposed to know which product is meant to be added to the user's cart? I'm not going to go in to the output of raw variables into HTML but you're going to want to look into escaping output, and just the mishmash combination of php and HTML is going to make this a bit of a nightmare to maintain or extend as you move forward. Ideally, you'd use a separate php file to gather your data and a templating system (twig is great, btw) for display.

 

If you continue forward with the code you've posted, first thing's first - use PDO instead of mysql_* functions. This will allow your site to still be functional later this year. You'll want to gather all the product data into an array at the top of your script, then print out the line items later on in the body of the page. You can do this is one of two ways: create a separate form for each item listed that contains a hidden field with the product ID as mac_gyver suggests, or change the name of the submit button to an array where the key is the product ID as such:

<input type="submit" class="..." name="my-add-button[product][<?= htmlspecialchars($all['id']); ?>]" value="add to cart" />

That way, when the form is submitted, you know what product the customer actually wants. There shouldn't be a need for the product name or price to be stored in the form as your processing script can grab that data from the database.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.