ajax无法在购物车中一键工作

ajax not working onclick in shopping cart

本文关键字:一键 工作 购物车 ajax      更新时间:2024-04-26

我正在使用免费下载的"Ustora"模板(电子商务的html主题)。同样,我正在处理单个产品页面中的"添加到购物车"按钮。点击后,它应该会启动一个ajax函数,将产品数据插入数据库中的购物车表中。但这个不起作用。

我正在用这个来学习制作电子商务网站。请尽快提供帮助,下面是我的代码。

单一产品页面.php

             <?php 
                $row = mysql_fetch_row(mysql_query("SELECT * FROM `products` where pid='".$_GET['proid']."' "));
                echo '<div class="row">
                    <div class="col-sm-6">
                        <div class="product-images">
                            <div class="product-main-img">
                                <img src="'.$row[5].'" alt="'.$row[2].'">
                            </div>
                            <div class="product-gallery">
                                <img src="'.$row[5].'" alt="'.$row[2].'">
                                <img src="'.$row[5].'" alt="'.$row[2].'">
                                <img src="'.$row[5].'" alt="'.$row[2].'">
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="product-inner">
                            <h2 class="product-name">'.$row[2].'</h2>
                            <div class="product-inner-price">
                                <ins>'.$row[3].'/- INR</ins> <del>'.$row[3].'/- INR</del>
                            </div>    
                            <form method="post">
                                <div class="quantity">
                                    <input type="number" size="4" class="input-text qty text" title="Qty" value="1" name="quantity" id="qty" min="1" step="1" max='.$row[6].'>
                                </div>
                              <button class="add_to_cart_button" onclick="addtocart('.$row[1].','.$row[2].','.$row[3].','.$buid.');">
                                    Add to cart
                                </button>
                            </form> 
                            <div class="product-inner-category">
                                <p>Category: <a href="">Summer</a>. Tags: <a href="">awesome</a>, <a href="">best</a>, <a href="">sale</a>, <a href="">shoes</a>. </p>
                            </div> 
                            <div role="tabpanel">
                                <ul class="product-tab" role="tablist">
                                    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Description</a></li>
                                    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Reviews</a></li>
                                </ul>
                                <div class="tab-content">
                                    <div role="tabpanel" class="tab-pane fade in active" id="home">
                                        <h2>Product Description</h2>  
                                        '.$row[4].'
                                        </div>
                                    <div role="tabpanel" class="tab-pane fade" id="profile">
                                        <h2>Reviews</h2>
                                        <div class="submit-review">
                                            <p><label for="name">Name</label> <input name="name" type="text"></p>
                                            <p><label for="email">Email</label> <input name="email" type="email"></p>
                                            <div class="rating-chooser">
                                                <p>Your rating</p>
                                                <div class="rating-wrap-post">
                                                    <i class="fa fa-star"></i>
                                                    <i class="fa fa-star"></i>
                                                    <i class="fa fa-star"></i>
                                                    <i class="fa fa-star"></i>
                                                    <i class="fa fa-star"></i>
                                                </div>
                                            </div>
                                            <p><label for="review">Your review</label> <textarea name="review" id="" cols="30" rows="10"></textarea></p>
                                            <p><input type="submit" value="Submit"></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>';
                ?>

Header.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Product Page - Ustora Demo</title>
    <!-- Google Fonts -->
    <link href='http://fonts.googleapis.com/css?family=Titillium+Web:400,200,300,700,600' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Roboto+Condensed:400,700,300' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Raleway:400,100' rel='stylesheet' type='text/css'>
    <script src="jquery.js"></script>

    <script type="text/javascript">
    function addtocart(pid,name,price,buid)
    {
        alert("addToCart function working");
    var pid = pid;
    var pname = name;
    var pprice = price;
    var pqty = document.getElementById("qty").value; //$(#qty).val();
    var buid = buid;
    //var cstid = $(#).val();
    $.ajax({
            type:"POST",
            url:"http://localhost/phpsales/insert-cart.php",
            data:{pid,pname,pprice,pqty,buid},
            cache:false,
            success:alert("Product Added Successfully")
            //error:function fail(){alert("Some technical error occured dufine product add to cart. Please try after some time.");}
        });
    }
    </script>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="css/owl.carousel.css">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="css/responsive.css">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body> 

我只想在"添加到购物车"按钮上启动addtocart()功能,点击后想在购物车中插入数据。

提前感谢

我认为问题出在将数据发送到新页面的数据中

function addtocart(pid,name,price,buid)
    {
    alert("addToCart function working");
    var pid = pid;
    var pname = name;
    var pprice = price;
    var pqty = document.getElementById("qty").value; //$(#qty).val();
    var buid = buid;
    //var cstid = $(#).val();
    $.ajax({
            type:"POST",
            url:"http://localhost/phpsales/insert-cart.php",
            data:{pid,pname,pprice,pqty,buid},//here is the problem 
            data:{pid:pid,pname:pname,pprice:pprice,pqty:pqty,buid:build},//this must be like this
            cache:false,
            success:alert("Product Added Successfully")
            //error:function fail(){alert("Some technical error occured dufine product add to cart. Please try after some time.");}
        });
    }

我认为您需要在调用addToCard()函数的参数周围加引号,如下所示:onclick="addtocart(/''.$row[1].'/',/''.$row[2]./'',/''.$row[3].'/',/''.$buid.'/');"