根据下拉列表中的选定项更新表单字段(jquery、PHP和MySQL)
我有一个数据库表,其中包含以下格式的额外预订:根据下拉列表中的选定项更新表单字段(jquery、PHP和MySQL),php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有一个数据库表,其中包含以下格式的额外预订: name:price 我从数据库中获取了一个预订附加服务的PHP数组,并创建了一个包含其名称的选择框,还显示了一个价格字段: <label class="label">Name</label> <label class="select"> <select id="name" name="name"> <?php foreach ($upsells as &$upsell) {
name:price
我从数据库中获取了一个预订附加服务的PHP数组,并创建了一个包含其名称的选择框,还显示了一个价格字段:
<label class="label">Name</label>
<label class="select">
<select id="name" name="name">
<?php
foreach ($upsells as &$upsell) {
echo '<option value="'.$upsell['name'].'">'.$upsell['name'].'</option>';
}
?>
</select>
</label>
<label id="pricet" class="label" style="display:none;">Price</label>
<label class="input" id="price" style="">
<input class="" id="price" type="text" pattern="\d+(\.\d{2})?" name="price" value="" >
</label>
json字符串是在调用get_upsells.php(我手动检查)时创建的,但在选择项目时,我看不到来自浏览器的任何ajax请求
我哪里出错了
编辑:在“GET”之后找到一个缺少的。我现在看到ajax请求,但无法确定如何更新“price”表单字段
编辑:JS函数现在如下所示:
$(document).ready(function() {
$("#name").change(function () {
var choice = jQuery(this).val();
$.ajax({
url:'/get_upsell.php?pid=<?php echo $urlcrypt->encrypt($pid);?>',
type:'GET',
data : {'id' : choice},
success : function(response) {
response = jQuery.parseJSON(response);
$('input[name=price]').val(response.price);
}
});
});
})
这会在价格字段中填充“Hello”,因此看起来像
response.price
…位未传递数据。在键入“GET”之后出现语法错误。 编辑:您需要解析到JSON对象
success : function(response) {
response = jQuery.parseJSON(response);
在此之后,要设置值,您需要删除引号
$('input[name=acct]').val(response.price);
编辑2:对于ajax,您必须echo
JSON,而不是return
(在php文件中)现在已修复:
$('#price').val(response[0].price);
你能在firebug或开发者工具上看到ajax响应吗?
success : function(response) {
response = jQuery.parseJSON(response);
$('input[name=acct]').val(response.price);
$('#price').val(response[0].price);