簡體   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