I'm trying to build a small e-commerce website, and now that I just made my jquery program calculate items on cart, I was trying to calculate also the total amount of these items and print it next to the first one. So when I check out my source code then network then my js file in google chrome, I see that the amount was calculated correctly but it is not displayed. Here is my code:
My html span: (where to display the total amount):
<span id="total12"><?php echo $_SESSION['total'];?></span>
My addpanier.php file :
<?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);
?>
And my jquery file :
(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);
use .html()
$('#total12').html(data.total1);
thank you folks for your time, my error was in my selector, in fact i was checking if a session variable is existing right before where i should display my stuff Ibetter go to bed...(it is 2 a.m in Morocco). :D Thank you all !