我在jQuery中有一个函数,它将值发送到PHP文件:
$.ajax({ //make ajax request to cart_process.php
url: "cart_process.php",
type: "POST",
dataType: "json", //expect json value from server
data: {
quantity: iqty,
product_code: icode,
color: icolor,
color_code: icolorcode
}
}).done(function(data) { //on Ajax success
$("#cart-info").html(data.items); //total items in cart-info element
button_content.html('Add to Cart'); //reset button text to original text
alert("Item added to Cart!"); //alert user
if ($(".shopping-cart-box").css("display") == "block") { //if cart box is still visible
$(".cart-box").trigger("click"); //trigger click to update the cart box.
}
}).fail(function(data) {
alert('failed to load')
})
这里AJAX请求由于某种原因失败并显示alert('failed to load');
。 Ajax正在请求cart_process.php
并在data
变量中发送一些值,这些值在cart_process.php
中处理,如下所示:
if (isset($_POST["quantity"]) && isset($_POST["product_code"]) && isset($_POST["color"]) && isset($_POST["color_code"]))
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$product_qty = filter_var($_POST["quantity"], FILTER_SANITIZE_NUMBER_INT); //product quantity
$color = filter_var($_POST["color"], FILTER_SANITIZE_STRING); //product color
$color_code = filter_var($_POST["color_code"], FILTER_SANITIZE_NUMBER_INT); //product color_code
$product = array();
$found = false;
//fetch item from Database using product code
$statement = $mysqli_conn->prepare("SELECT familyName, productPrice FROM productsMaster WHERE pid = ? LIMIT 1");
$statement->bind_param('s', $product_code);
$statement->execute();
$statement->bind_result($familyName, $productPrice);
while ($statement->fetch()) {
$new_product = array( array('colorname'=> $color, 'name'=> $familyName, 'price'=> $productPrice, 'code'=>$product_code, 'qty'=>$product_qty)); //prepare new product
if(isset($_SESSION["products"]))
{
foreach ($_SESSION["products"] as $cart_itm) //loop though items
{
if($cart_itm["code"] == $product_code && $cart_itm["colorname"] == $color)
{ //if item found in the list, update with new Quantity
$product[] = array('colorname'=>$color, 'name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=> $cart_itm["price"]);
$found = true;
}
else
{
//else continue with other items
$product[] = array('colorname'=>$cart_itm["colorname"], 'name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=> $cart_itm["price"]);
}
}
if (!$found)
{ //we did not find item, merge new product to list
$_SESSION["products"] = array_merge($product, $new_product);
}
else
{
$_SESSION["products"] = $product; //create new product list
}
}
else
{ //if there's no session variable, create new
$_SESSION["products"] = $new_product;
die(json_encode(array('items'=>1)));
}
}
$total_items = count($_SESSION["products"]); //count items in variable
die(json_encode(array('items'=>$total_items))); //exit script outputing json data
}
我需要检查为什么AJAX成功无效。如何在PHP中调试或检查这些值,因为echo
在屏幕上没有显示任何内容?
答案 0 :(得分:0)
您可以从浏览器源选项卡和控制台进行调试。