繁体   English   中英

Ajax无法正常工作onclick在购物车中

[英]ajax not working onclick in shopping cart

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

我正在与此一起学习使电子商务网站。 请最早提供帮助的是我的以下代码。

单产品-page.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.'/');"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM