基于使用ajax选择的值填充多个字段
populating multiple fields based on value selected with ajax
我试图在"选择"从数据库生成的项目后自动填充两个字段。我不确定我在哪里犯的错。我也使用了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);
?>
相关文章:
- 如何使用与单个查找字段相关的信息填充字段
- 动态填充字段的Jquery验证
- 使用另一个字段的日期+14天自动填充字段
- Mongoose,按填充字段对查询进行排序
- 使用 Codeigniter 和 jquery 在 Modal Bootstrap 3 中的 DB 中填充字段
- 填充字段后更改不透明度
- 根据下拉选择(Javascript / HTML)动态填充字段
- JavaScript 重置 PHP/AJAX 中的填充字段
- 对填充字段的 MongoDB 查询
- 根据 $_POST 值填充字段
- Javascript 自动完成表单并在预填充字段中附加文本
- Jquery 未完成填充字段
- 在客户端使用 javascript、ajax 或 php 计算填充字段的数量
- 使用地理位置坐标填充字段
- 根据同时在另一个字段中输入的内容自动填充字段
- 如何将struts1与angularjs混合以自动填充字段
- 填充字段,该字段是单个查询中嵌入数组的属性
- Phantom.js填充字段,点击提交,现在只能访问下一页
- 如何在预填充字段时清除iOS占位符JS文本
- 我的response.jsp赢了'无法正确填充字段