基于使用ajax选择的值填充多个字段

populating multiple fields based on value selected with ajax

本文关键字:填充 字段 于使用 ajax 选择      更新时间:2023-09-26

我试图在"选择"从数据库生成的项目后自动填充两个字段。我不确定我在哪里犯的错。我也使用了firebug,但它没有显示任何错误消息。在我从下拉菜单中选择一个项目后,它就不会填充了。请帮帮我,让我知道我哪里做错了。

这是脚本:

<script type="text/javascript" language="javascript"> 
$(function () {
    $('#description').bind('input', function () {
        $(this).val() // get  value
        $.ajax({
            type: 'POST',
            url: 'orderAuto.php',
            data: {
                url: $('#description').val()
            },
            dataType: 'json',
            success: function (data) //on recieve of reply
            {
                var skuId = data[0]; 
                var unitPrice = data[1];
                $('#sku_1').val(skuId);
                $('#uPrice_1').val(unitPrice);
            }
        });
    });  
});  
</script>

这是我的表格,包含数据库中的字段和部分:

<form name="form" method="get">
<table width="70%" border="5" align="center"><tr>
<th scope="row">Item Name</th>
<th scope="row">Item SKU</th>
<th scope="row">Quantity</th>
<th scope="row">Special Note</th>
<th scope="row">Unit Price</th>
<th scope="row">Total Price</th>
</tr>
<tr>
<th scope="row">
<?php
include('connect.php');
$result = mysqli_query("SELECT description FROM products") 
            or die(mysqli_error());
print '<select name="description" id="description" value="description">';
print '<option value="" disabled selected>Please Select A Product</option>';
while ($info = mysqli_fetch_array($result))
{
        $p = $info["description"];
        $p = htmlspecialchars($p);
        printf('<option value="%s">%s</option>', $p, $p);
}
print '</select>';
?>
</th>
<th scope="row"><input name="sku_1" id="sku_1" readonly /></th>    
<th scope="row"><input name="qty_1" /></th>
<th scope="row"><input name="note_1" /></th>  
<th scope="row"><input name="uPrice_1" id="uPrice_1" readonly /></th>
<th scope="row"><input name="tPrice_1" readonly /></th>
</tr>
</table>
<input type="submit"/>
</form>    

这是orderAuto.php:

<?php
    include('connect.php');
    $p = $_POST['description'];
    $result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'");
    $array = mysqli_fetch_array($result);
    echo json_encode($array);
?>   

更新

<script type="text/javascript" language="javascript"> 
$(function () {
    $('#description').change(function () {
        $.ajax({
            type: 'POST',
            url: 'orderAuto.php',
            data: {
                description: $(this).val()
            },
            dataType: 'json',
            success: function (data) //on recieve of reply
            {
                var skuId = data[0]; 
                var unitPrice = data[1];
                $('#sku_1').val(skuId);
                $('#uPrice_1').val(unitPrice);
            }
        });
    });  
});  
</script>

<?php
    include('connect.php');
    $p = mysqli_real_escape_string($_POST['description']); // should be doing this
    $result = mysqli_query("SELECT sku_id, unit_price FROM products WHERE description= '".$p."'");
    $array = mysqli_fetch_array($result);
    echo json_encode($array);
?>