簡體   English   中英

jQuery計算總金額但不顯示

[英]jquery calculate the total amount but do not show it

我正在嘗試建立一個小型電子商務網站,現在我使我的jquery程序可以計算購物車中的商品,因此我還試圖計算這些商品的總金額並將其打印在第一個商品的旁邊。 因此,當我檢查我的源代碼然后聯網,然后在谷歌瀏覽器中查看我的js文件時,我看到金額計算正確,但未顯示。 這是我的代碼:

我的html span:(顯示總金額的位置):

<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);

謝謝大家的寶貴時間,我的錯誤出在我的選擇器中,實際上我正在檢查會話變量是否存在,然后才應該顯示我的東西。Ibetter上床睡覺……(摩洛哥時間凌晨2點)。 :D謝謝大家!

使用.html()

 $('#total12').html(data.total1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM