dqz86173 2016-10-11 17:28
浏览 51
已采纳

如何使用单个AJAX调用更新多个HTML表单字段? [关闭]

I have:

<tr>
    <td>Product</td>
    <!-- line below generates a select box with id="product" -->
    <td id="product"><?=$this->formSelect($form->get('product'));?></td>
</tr>
<tr>
    <td>Description</td>
    <td><input type="text" name="Description" id="Description" /></td>
</tr>
<tr>
    <td>Quantity</td>
    <td><input type="text" name="Quantity" id="Quantity" /></td>
</tr>
<tr>
    <td>Price</td>
    <td><input type="text" name="Price" id="Price" /></td>
</tr>

Question

When using onupdate feature of jQuery, that is tied to event of changing value of "product" select box, how do I update Description, Quantity and Price fields.

What I have now is this:

$("#product").change(function() {
    $.ajax({
        type : "POST",
        url : "updatedescription.php",
        data : 'product_id=' + $(this).val(),
        cache : false,
        success : function(html) {
            $("#Description").html(html);
        }
    });

    return false;
});

But it updates Description only.

PHP Code

function loadDescriptionByProduct()
{
    $product = filter_var($_POST['product_id'], FILTER_SANITIZE_STRING);
    $description = $this->repository->getDescriptionByProduct($product);
    echo $description;
}

function getDescriptionByProduct(string $product)
{
    $sql = "SELECT description FROM product where product=?";
    $result = db_param_query($sql, $product);
    $row = db_fetch_array($result);
    $description = $row['description'];
    return $description;
}
  • 写回答

1条回答 默认 最新

  • dougaxing8673 2016-10-11 20:08
    关注

    You need to call a single resource like getProductDetails.php and get there all the information related to the product you want.

    PHP

    function getProductDetails(string $product)
    {
        $sql = "SELECT price, description FROM product where product=?";
        $result = db_param_query($sql, $product);
        $row = db_fetch_array($result);
    
        $response = array(
            'price' => $row['price'],
            'desc' => $row['description']
        );
    
        return json_encode($response); //return a json response.
    }
    

    Javascript

    $("#product").change(function() {
        $.ajax({
            type : "POST",
            url : "getProductDetails.php",
            data : 'product_id=' + $(this).val(),
            cache : false,
            success : function(response) {
                var parsedResponse = $.parseJSON(response);
    
                $("#Description").html(parsedResponse.desc);
                // ...
                $("#Price").html(parsedResponse.price);
            }
        });
    
        return false;
    });
    

    Also, you can use Content-Type: application/json response header and indicate to jQuery that this is JSON by using the dataType: 'json' parameter. (and remove $.parseJSON())

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Qt 不小心删除了自带的类,该怎么办
    • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
    • ¥15 有没有哪位厉害的人可以用C#可视化呀
    • ¥15 可以帮我看看代码哪里错了吗
    • ¥15 设计一个成绩管理系统
    • ¥15 PCL注册的选点等函数如何取消注册
    • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
    • ¥15 Python爬虫程序
    • ¥15 crypto 这种的应该怎么找flag?
    • ¥15 代码已写好,求帮我指出错误,有偿!