如何调试和检查由ajax传递的php中的值

时间:2015-07-15 06:49:20

标签: javascript php jquery ajax json

我在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在屏幕上没有显示任何内容?

1 个答案:

答案 0 :(得分:0)

您可以从浏览器源选项卡和控制台进行调试。