【发布时间】:2016-09-18 09:01:20
【问题描述】:
我正在尝试建立一个小型电子商务网站,现在我刚刚让我的 jquery 程序计算购物车上的物品,我还试图计算这些物品的总量并将其打印在第一个旁边.因此,当我检查我的源代码然后联网然后我在谷歌浏览器中的 js 文件时,我看到金额计算正确但未显示。这是我的代码:
我的html跨度:(在哪里显示总金额):
<span id="total12"><?php echo $_SESSION['total'];?></span>
我的 addpanier.php 文件:
<?php
session_start();
require 'connexion.php';
require 'panier.php';
$json=array('error' => true);
$panier=new panier();
try {
$con1 = new myPDO();
}
catch (PDOException $e) {
echo "<br/> Erreur: ".$e->getMessage()."<br/>" ;
die() ;
}
if (isset($_GET['id'])) {
$bdd = $con1->prepare("SELECT * FROM article a,promotion p WHERE a.id=p.id_article HAVING a.id=?") ;
$bdd->bindValue(1,$_GET["id"]);
$bdd->execute();
$result = $bdd->fetch(PDO::FETCH_ASSOC);
$tot2=0;
if (empty($result)) {
$json['message']="produit introuvable!";
}else{
$panier->add($result["id"]);
$json['error']= false;
if (isset($_SESSION['panier'])) {
$tot=0;
$tot=$result["prix"]*$_SESSION['panier'][$result["id"]];
$b=$tot-$tot*($result["rabais"]/100)+$result["tax"];
$tot2+=$b;
}else{
$tot=0;
$tot=$result["prix"];
$b=$tot-$tot*($result["rabais"]/100)+$result["tax"];
$tot2+=$b;
}
$json['total1']=$tot2;
$json['panier']= array_sum($_SESSION['panier']);
$json['message']='produit bien ajoute a votre panier !';
}
}else{
$json['message']="pas de produits a ajouter au panier";
}
echo json_encode($json);
?>
还有我的 jquery 文件:
(function($){
$('.addpanier').click(function(event){
event.preventDefault();
$.get($(this).attr('href'),{},function(data){
if (data.error) {
alert(data.message);
}else{
if(confirm(data.message + '. Voulez vous consulter votre panier?')){
location.href="product_summary.php";
}else{
$('#panier12').empty().append(data.panier);
$('#total12').empty().append(data.total1);
}
}
},'json');
return false;
});
})(jQuery);
【问题讨论】:
标签: javascript php jquery ajax e-commerce