Php 使用ajax更新HTML输入

Php 使用ajax更新HTML输入,php,jquery,html,mysql,ajax,Php,Jquery,Html,Mysql,Ajax,我们有一个数据库: ID Name Price 1 product1 200 2 product2 300 3 product3 400 我的表格: <form action="test.php" method="post" /> <input type="text" name="search" id="search" placeholder="enter name product" /> <div id="result

我们有一个数据库:

ID    Name     Price
1   product1    200
2   product2    300
3   product3    400
我的表格:

<form action="test.php" method="post" />
<input type="text" name="search" id="search" placeholder="enter name product" />
<div id="result">display result form ajax</div>
<label>price</label>
<input type="text" name="price" id="price" readonly" />
<input type="submit" name="submit" />
</form>
当用户在搜索输入中搜索产品名称时,我们向search.php发送一个ajax请求,并显示类似于该关键字的产品 search.php:

connection db and other code...
if(product) {
echo '<input type="radio" name="product" id="product" value="'.$result['name'].'" />'.$result['name'];
}

用户选择该选项,现在如何用产品的价格更新价格输入?

您的“search.php”应该返回价格的json和html

{
    price: 200,
    html: "......"
}
在成功功能中,您可以更新价格

success: function(json){
    $('#price').val(json.price);
    $("ul#results").html(json.html);
}

您可以在数据属性->echo.$result['name'];中添加价格;。然后在js代码中,$'ulresults'。单击,'.product',函数{$'price.val$this.data'price';};。请注意,您的每个单选按钮都有id=product,这会创建无效的html,因此我改为class=product以在js->'中使用。product“谢谢。它起作用了,用这种方式获取数据的另一个问题是securemy way?不确定您所说的安全是什么意思。您的代码看起来很好。只要您在搜索数据库时正确地清理了数据,您就会没事,因为您提供的只是连接数据库和其他代码。。。
success: function(json){
    $('#price').val(json.price);
    $("ul#results").html(json.html);
}