簡體   English   中英

如何調試和檢查由ajax傳遞的php中的值

[英]how to debug and check values in php which are passed by ajax

我在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在屏幕上未顯示任何內容?

您可以從瀏覽器的“源”選項卡和控制台進行調試。

暫無
暫無

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

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