繁体   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