我正在尝试建立一个小型电子商务网站,现在我只是让我的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);
答案 0 :(得分:0)
谢谢大家的时间,我的错误是在我的选择器中,事实上我正在检查会话变量是否存在之前我应该显示我的东西Ibetter去睡觉...(现在是凌晨2点在摩洛哥)。 :D谢谢大家!
答案 1 :(得分:-1)
使用.html()
$('#total12').html(data.total1);