Lau_1 12 Junior Poster in Training

Hello, i not sure what is the problem with my code. Below is my code.

                        <?php 
                        if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){
                            $total          = 0;

                            foreach($_SESSION["products"] as $product){
                                $product_name = $product["name"]; 
                                $product_price = $product["price"];
                                $product_code = $product["product_code"];
                                $product_qty = $product["product_qty"];
                                $currency = "RM ";

                                $img = mysqli_query($link, "SELECT img FROM internet_shop where id=$product_code");
                                $row = mysqli_fetch_assoc($img);

                                $item_price     = sprintf("%01.2f",($product_price));  // price x qty = total item price
                                $subtotal       = sprintf("%01.2f",($product_price * $product_qty)); //Multiply item quantity * price
                                $total          = sprintf("%01.2f",($total + $subtotal)); //Add up to total price
                            ?>
                            <tr>
                            <form id="update_info_cart<?php echo $product_code?>" class="form-horizontal form-label-left">
                                <input name="update" type="hidden" value="update">
                                <td><img src="<?php echo "adminPanel/MenuPicture/".$row['img']?>" alt="<?php echo $product_name ?>"></td>
                                <td colspan="2"><?php echo $product_name ?></td>
                                <td><input type="number" value="<?php echo $product_qty ?>"  name="product_qty" class="form-control" min="1" max="5"></td>
                                <td><?php echo $currency. $item_price?></td>
                                <td><?php echo $currency. $subtotal?></td>
                                <td><a class="update-item<?php echo $product_code?>"><i class="fa fa-refresh"></i></a> </td>
                                <script>
                                    $(document).ready(function(){
                                         $('a.update-item'+<?php echo $product_code?>).click(function(e) {
                                            var form_data = $('#update_info_cart'+<?php echo $product_code?>).serialize();

                                            /*$.ajax({ //make ajax request to cart_process.php
                                                url: "update_cart_process.php",
                                                type: "POST",
                                                dataType:"json", //expect json value from server
                                                data: form_data
                                            }).done(function(data){ //on Ajax success
                                                alert(<?php echo $product_code?>);

                                            })
                                            e.preventDefault();*/

                                            alert(form_data);
                                        });
                                    });
                                </script>

                                <td><a class="remove-item" data-code="<?php echo $product_code?>"><i class="fa fa-trash-o"></i></a> </td>
                            </form>
                            </tr> 
                            <?php
                            }
                        }
                        ?>

All the cart item store in the $_SESSION["products"] and i try to update cart by using the code above. For example

Untitled.png

The picture shown is the view cart table. the $_SESSION["products"] contain 3 items. And when i try to check the what info that send through ajax to process, by alert(form_data);, it prompt the first row empty, second and third is correct. I dont know what happened, can anyone help? sorry for my poor english.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.