Javascript ajax在购物车中单击时不起作用

Javascript ajax在购物车中单击时不起作用,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我正在使用免费下载的“Ustora”模板(电子商务的html主题)。同样,我正在使用单一产品页面中的“添加到购物车”按钮。单击后,它应该启动一个ajax函数,将产品数据插入数据库中的my cart表中。但是这个不起作用 我正在与此学习制作电子商务网站。请帮助我,因为下面是我的代码 单一产品页面.php 我认为问题在于数据,您将数据发送到新页面 function addtocart(pid,name,price,buid) { alert("addToCart function w

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

我正在与此学习制作电子商务网站。请帮助我,因为下面是我的代码

单一产品页面.php


我认为问题在于数据,您将数据发送到新页面

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./'./);“

您检查控制台错误了吗?您是否收到“addtocart函数工作”警报?不,我没有收到警报(“addtocart函数工作”)消息。即使是在“添加到购物车按钮”点击后加载相同的页面。这太可疑了。在inspect elements(chrome浏览器)中,addtocart()函数显示其中的数据。像这样onclick=“addtocart(10001,三星Galaxy s5-20152400,8kufj1umpgn3537n8qmh0hr1e3)”;但这一个甚至没有提醒第一条消息警报(“addtocart功能工作”);。引用您的参数值,然后您应该修复@Vicky在inspect elements(chrome浏览器)addtocart()函数中指出的显示其中数据的内容。像这样onclick=“addtocart(10001,三星Galaxy s5-20152400,8kufj1umpgn3537n8qmh0hr1e3);”我认为现在应该可以了,只要试着在值周围加上引号并正确转义即可
<!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> 
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.");}
        });
    }