jQuery Ajax Post Bootstrap Popover问题

时间:2013-09-01 14:19:40

标签: php jquery ajax twitter-bootstrap popover

我有一个电子商务系统。我正在将我的产品添加到购物车中,就像截屏enter image description here

一样

当我点击添加到购物车按钮时。我正在显示一个购物车通知,它是bootstrap popover。主要问题是,当我决定使用其他产品时,我会收到相同的通知。

我写的是“产品A已添加”。当我在每个不同的产品中添加它时,会添加写入产品A.添加正确但通知错误。

这是我的jQuery代码......

$('.quick-add-cart').click(function(){ //Sepete Ekleme İşlemi yapıyoruz.
     $('#cart-popover').attr('data-content','');
     productID  = $(this).parents('.productbit').attr('id');
     $.ajax({
          type: "POST",
          url: "ajax/add_to_cart.php",
          dataType: 'json',
          data: {            
              productID : productID
          },
          error: function(jqXHR, exception) {
              if (jqXHR.status === 0) {
                   alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrol ediniz.');
          } else if (jqXHR.status == 404) {
              alert('Sayfa bulunamadı. [404]');
          } else if (jqXHR.status == 500) {
              alert('Sunucu Hatası [500].');
          } else if (exception === 'parsererror') {
              alert('Requested JSON parse failed.'+ jqXHR.responseText);
          } else if (exception === 'timeout') {
              alert('Time out error.');
          } else if (exception === 'abort') {
              alert('Ajax istemi durduruldu.');
          } else {
              alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
          }
          },
          success: function (data) {
              if (data.insert_count > 0) {
                  cart_old_count = $('#cart_count').text();
                  cart_new_count = parseInt(cart_old_count,10) + data.insert_count;
                      $('#cart_count').text(cart_new_count);
              }
                  $('#cart-popover').attr('data-content',data.queryresult).popover('show');
                  setTimeout(function() {
                      $('#cart-popover').popover('hide');
                  }, 5000);
              }
                            });
                       });

这是我的add_to_cart.php

require_once '../includes/class/class.cart.php';
        include_once '../locale.php';

        session_start();
        $C = new Cart(); 

//Sepete Ürün Ekliyoruz
if (isset($_POST['productID'])){
    if (!isset($_SESSION['userid'])) {
        $jsonData['queryresult'] = "Sepete Ekleme Yapabilmek için Bayi Girişi Yapmalısınız !";
    } else {
        $productid = $_POST['productID'];
        $product   = $C->get_product_fields('product', $productid);

        $jsonData['update_count'] = 0;
        $jsonData['insert_count'] = 0;

        $cart_error = FALSE;

        if ($C->is_in_cart($productid)) {
            if ($C->update_cart($productid))
                $jsonData['update_count'] += 1;
            else {
                $jsonData['queryresult'] = $C->cart_errors($productid);
                $cart_error = TRUE;
            }
        } else {
            if ($C->add_to_cart($productid))
                $jsonData['insert_count'] += 1;
            else {
                $jsonData['queryresult'] = $C->cart_errors($productid);
                $cart_error = TRUE;
            }
        }

        if ($cart_error === FALSE) {
            if ($jsonData['insert_count'] == 1){
                $jsonData['queryresult'] = "Bir paket ".$product." eklendi.";
            }

            if ($jsonData['update_count'] == 1){
                $jsonData['queryresult'] = "Bir paket daha ".$product." eklendi.";
            }

            if ($jsonData['insert_count'] == 0 && $jsonData['update_count'] == 0){
                $jsonData['queryresult'] = "Ürünü sepete ekleme sırasında hata oldu.";
            }
        }
    }
        header('Content-type: application/json');
        echo json_encode($jsonData);
}

1 个答案:

答案 0 :(得分:0)

相关问题:Twitter bootstrap js popover content doesn't update when data-content attribute is updated

解决问题需要进行两项更改:

  1. 您应该使用.attr('data-content', data.queryresult)

  2. 而不是.data('content', data.queryresult)
  3. 在更新内容之前,您需要使用.popover('destroy'),这可以防止popover因异步处理而丢失。

  4. 这是一个示例代码,我使用setTimeout来模拟Ajax调用:

    function displayPopover(msg) {
      $('#cart-popover').popover('destroy');
      $('#cart-popover').data('content',msg).popover('show');
      setTimeout(function() {
          $('#cart-popover').popover('hide');
      }, 5000);
    }
    
    // add first product to cart
    setTimeout(function() { displayPopover('product 1') }, 2000);
    
    // add second product to cart
    setTimeout(function() { displayPopover('product 2') }, 4000);
    

    您可以在此处看到它:http://jsbin.com/UXAxEBE/1/

    提示:如果你有这种类型的错误,你不知道问题是在php还是javascript方面,你应该尝试在你的javascript中console.log(data.queryresult)来验证php返回正确的内容,并且你的问题是在客户端。