2017-06-10 49 views
0

我已經創建了一個查詢它工作之前,但是當我試圖以json格式獲取數據,雖然它不工作,我不知道爲什麼它不顯示數據作爲回報這裏是我的碼。當我檢查控制檯返回數據不顯示,但即​​使它不是處理我認爲,但我無法糾正問題的錯誤。任何人都可以幫助我。無法顯示通過ajax調用返回數據

$(document).ready(function(e) { 
    $("#addcart").on('submit', (function(e) { 
     var oldval = $('#crtcount').text(); 
     var form  = this; 
     var formData = new FormData(this); 
     $.ajax({ 
      url   : "includes/get_data.php", 
      type  : "POST", 
      data  : formData, 
      contentType : false, 
      cache  : false, 
      processData : false, 
      dataType : "JSON", 
      success  : function(data) { 
       var data = $.parseJSON(data); 
       $('#crtcount').html(parseInt(oldval)+1); 
       $('#crtcount1').html(parseInt(oldval)+1); 
       $('#cart_dt').html(data.cart); 
       console.log(data.message); 
      } 
     }); 
    })); 
}); 

他是我所處理的數據

$qty = $_POST['qty']; 
$pid = $_POST['pid']; 
echo $cart->add_to_cart($pid); 

$data['message'] = "<div class='message'>Your product is added to cart</div>"; 
$data['cart']  = ''; 
foreach($_SESSION['cart']['pid'] as $content) { 
    $prod_id   = $content['prod_id']; 
    $products   = bgMysqlSelect("SELECT * FROM bg_products WHERE pid = '$prod_id'"); 
    $data['cart'] .= '<div class="items-sm"><div class="img-sm"><img src="../includes/uploads'.$products['pimage'].'" /></div>'; 
    $data['cart'] .= '<div class="cart-inf-sm"><h4>'.$products['ptitle'].'</h4>'; 
    $data['cart'] .= '<p><span>price :</span>$'.$products['pmsrp'].'</p>'; 
    $data['cart'] .= '<p><span>Qty :</span>'.$content['qty'].'</p>'; 
    $data['cart'] .= '<a href=""><i class="fa fa-times"></i></a></div></div>'; 
} 

echo json_encode($data); 
+0

你可以檢查F12網絡t ab瞭解你的php工作是否 – hasan

+0

@hasan我檢查了網絡ab,我只看到這個我不知道如何檢查網絡選項卡上的bug –

+0

按f12然後點擊網絡選項卡,然後點擊XHR過濾器,你會看到XHR請求 – hasan

回答

1

在你的PHP文件中的問題創造了我的PHP代碼..

:不迴應任何事情與json_encode()

2nd:評論您的foreach lo OP 這可能是問題的原因

所以你的代碼應該是這樣的

$qty = $_POST['qty']; 
$pid = $_POST['pid']; 
//echo $cart->add_to_cart($pid); 

$data['message'] = "<div class='message'>Your product is added to cart</div>"; 
$data['cart']  = ''; 
/*foreach($_SESSION['cart']['pid'] as $content) { 
    $prod_id   = $content['prod_id']; 
    $products   = bgMysqlSelect("SELECT * FROM bg_products WHERE pid = '$prod_id'"); 
    $data['cart'] .= '<div class="items-sm"><div class="img-sm"><img src="../includes/uploads'.$products['pimage'].'" /></div>'; 
    $data['cart'] .= '<div class="cart-inf-sm"><h4>'.$products['ptitle'].'</h4>'; 
    $data['cart'] .= '<p><span>price :</span>$'.$products['pmsrp'].'</p>'; 
    $data['cart'] .= '<p><span>Qty :</span>'.$content['qty'].'</p>'; 
    $data['cart'] .= '<a href=""><i class="fa fa-times"></i></a></div></div>'; 
}*/ 

echo json_encode($data); 

附加信息:,而你從爵士到PHP沒有input[type=file],而不是通過文件你可以用.serialize()代替new formData()

$(document).ready(function(e) { 
    $("#addcart").on('submit', (function(e) { 
     e.preventDefault(); 
     var oldval = $('#crtcount').text(); 
     var form  = $(this); 
     var formData = $(this).serialize(); 
     $.ajax({ 
      url   : "includes/get_data.php", 
      type  : "POST", 
      data  : formData, 
      //contentType : false, 
      //cache  : false, 
      //processData : false, 
      dataType : "JSON", 
      success  : function(data) { 
       //var data = $.parseJSON(data); // I think there is no need for this line 
       $('#crtcount').html(parseInt(oldval)+1); 
       $('#crtcount1').html(parseInt(oldval)+1); 
       $('#cart_dt').html(data.cart); 
       console.log(data.message); 
      } 
     }); 
    })); 
}); 
+0

非常感謝你真的幫我 –

+0

onc e最後一個問題,我必須運行此功能,所以如果我不寫回聲會運行,因爲我只是爲了將產品添加到購物車$ cart-> add_to_cart($ pid); –

+0

@UsmanKhan確定你可以使用它..如果這個功能已經可以將你的數據保存到數據庫..我不知道'add_to_cart()'函數裏面是什麼,但你可以檢查https://stackoverflow.com/questions/10424377/inserting-into-database-using-a-pdo .. –