繁体   English   中英

jQuery $ .ajax()函数未正确运行POST请求

[英]JQuery $.ajax() function not running POST request accurately

我有以下HTML

<select id="options" title="prd_name1" name="options" onblur="getpricefromselect(this);" onchange="getpricefromselect(this);"></select>

<input type="text" id="prd_price" title="prd_price1" name="prd_price">

我从系统数据库中动态更新了<select></select>并添加了一些产品及其各自的ID。 我编写了一个JQuery / Javascript函数,该函数假定使用$ .ajax()从选择菜单将该产品ID发送到php页面,并从数据库中获取其相应价格,并用该价格填充<input>

我的jQuery如下:

function getpricefromselect(x){

    var value=$(x).val();
    var title=$(x).attr('title');

    if(title.length == 9){
        var len=Number((title).substr(-1,1));    

        if(value.length>0){
            $.ajax({
                type:'POST',
                url:'getpricefromselect.php',
                data:{productid: value},
                success:function(data){
                    $("[title=prd_price"+len+"]").val(data).show();
                }
            })
        }
    }
};

并且“ getpricefromselect.php”中的php如下:

$productid="";
$Price="";
$productid=$_POST["productid"];
$selectProduct="SELECT InventoryProductPrice FROM InventoryProductsList WHERE InventoryProductID = '{$productid}';";
$selectProduct_query = mysqli_query($connection, $selectProduct);
if(!$selectProduct_query){
    die ("Database query for selecting Product Price failed.");
}
while ($selectProduct_array = mysqli_fetch_assoc($selectProduct_query)){
    $Price= $selectProduct_array["InventoryProductPrice"];
}
echo $Price;

问题是当我从PHP文件中回显$ Price时,jQuery $ .ajax()用空白项目填充<input> ,但是如果我回显$ productid,它将使用准确的productid填充<input> 我的产品ID对每个产品都是唯一的,当我在与javascript相同的页面上调用相同的php函数时,它将返回准确的价格。 谁能告诉我,如果从PHP回显了$ Price,为什么$ .ajax()返回一个空白项,而从php回显了$ productid却没有给出一个空白项。

如果我将实际产品ID手动添加到php文件,则php回显的$ Price是准确的。 如果它是通过POST从$ .ajax()发送的,则它似乎没有被php正确运行。

如果您使用开发人员控制台查看查询带来了什么答案,或者ajax请求遇到了什么问题,那将是很好的。

另一方面,您可以尝试这样

$(function(){
    $("#options").change(function()){
        getpricefromselect($(this).val());
    });
})


function getpricefromselect(x){

    var obj = {
        productid:x
    }

    $.post('getpricefromselect.php',obj)
    .done(function(resp){
        if(resp){
            $("#prd_price").val(resp)
        }
    })
    .fail(function(err){

    })

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM