0

I have a task to submit but i don't have much practice on php so i got help from watching youtube video and wrote this code. When i click on add to cart button it's supposed to show some message and then added to cart automatically but neither of them is working for me. Here's my products page:

<?php
require_once "dbconfig.php";
$select_stmt=$db->prepare("SELECT * FROM product"); 
$select_stmt->execute();
while($row=$select_stmt->fetch(PDO::FETCH_ASSOC))
{
?>
    <div class="col-lg-4 col-md-6 mb-4">
        <div class="card h-100" style="width: 250px; height:40px;">
            <a href="#"><img class="card-img-top" src="images/<?php echo $row['product_image']; ?>" width="50px" height="200px"></a>
      
            <div class="card-body">
                <h4 class="card-title text-primary"><?php echo $row['product_name']; ?> </h4>
                <h5><?php echo number_format($row['product_price'],2); ?>/-</h5>
            </div>
      
            <div class="card-footer">
                <form class="form-submit">
                    <input type="hidden" class="pid" value="<?php echo $row['product_id']; ?>">
                    <input type="hidden" class="pname" value="<?php echo $row['product_name']; ?>">
                    <input type="hidden" class="pprice" value="<?php echo $row['product_price']; ?>">
                    <input type="hidden" class="pimage" value="<?php echo $row['product_image']; ?>">
                    <input type="hidden" class="pcode" value="<?php echo $row['product_code']; ?>">
                    <button id="addItem" class="btn btn-success btn-md">Add to Cart</button>
                </form>
            </div>
        </div>
    </div>
<?php
}
?>

I added add to cart functionality using ajax:

<script type="text/javascript">
$(document).ready(function(){ 
    $(document).on("click", "#addItem", function(e){    
        e.preventDefault();
        var form = $(this).closest(".form-submit");
        var id = form.find(".pid").val();
        var name = form.find(".pname").val();
        var price = form.find(".pprice").val();
        var image = form.find(".pimage").val();
        var code = form.find(".pcode").val();
        
        $.ajax({
            url: "action.php",
            method: "post",
            data: {pid:id, pname:name, pprice:price, pimage:image, pcode:code},
            success:function(response){
                $(".alert-message").html(response);
                window.scrollTo(0,0);
                load_cart_item_number();
            } 
        });
    }); 
});
</script>

This code adds the item and also displays a message the message.

<?php
require_once "dbconfig.php";

if(isset($_POST["pid"]) && isset($_POST["pname"]) 
    && isset($_POST["pprice"]) &&isset($_POST["pimage"]) 
    && isset($_POST["pcode"])) 
{
    $id = $_POST["pid"]; 
    $name = $_POST["pname"]; 
    $price = $_POST["pprice"]; 
    $image = $_POST["pimage"];
    $code   = $_POST["pcode"]; 
    $qty = 1 ;

    $select_stmt=$db->prepare("SELECT product_code FROM cart WHERE product_code=:code");
    $select_stmt->execute(array(":code"=>$code));  
    $row=$select_stmt->fetch(PDO::FETCH_ASSOC);

    $check_code = $row["product_code"];
    
    if(!$check_code) {
        $insert_stmt=$db->prepare("INSERT INTO    cart
                (product_name,product_price,product_image,quantity,
                total_price,product_code) 
            VALUES (:name,:price,:image,:qty,:ttl_price,:code)"); 

        $insert_stmt->bindParam(":name",$name); 
        $insert_stmt->bindParam(":price",$price);   
        $insert_stmt->bindParam(":image",$image);   
        $insert_stmt->bindParam(":qty",$qty);    
        $insert_stmt->bindParam(":ttl_price",$price);   
        $insert_stmt->bindParam(":code",$code); 
        $insert_stmt->execute();
            
        echo '<div class="alert alert-success alert-dismissible mt-2">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Item added to your cart</strong> 
              </div>';              
    } else {
        echo '<div class="alert alert-danger alert-dismissible mt-2">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Item already added to your cart!</strong> 
            </div>';
    }

I know this is a really excessive code but I'm basically a newbie so please let me know if there's any mistake or error. There are also other php files i have made for this so let me know if you want to see it too.

Fatima
  • 49
  • 5
  • If you look at the PHP Manual the `isset()` function can take multiple parameter, rather than writing 5 isset()'s – RiggsFolly Apr 29 '23 at 15:38
  • 1
    Hi, one issue is that your form is under loop and you are using same id `addItem` for all items so your jquery code will work for first element with that id. Instead change that to `class`. – Swati Apr 29 '23 at 15:39

0 Answers0