gpt4 book ai didi

php - 为两个单独的 DIV 标签分配 AJAX 响应

转载 作者:行者123 更新时间:2023-12-01 07:52:24 24 4
gpt4 key购买 nike

这是我的ajax调用代码..

<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();

$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
$("#desc").val(data);
$("#bal").val(data);
}
});
});

});
</script>

include "db.php";
$itemcode=$_POST['itemcode'];

$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
echo $row['Item_Desc'];
echo $row['Balance_Stock'];
}

这是简单的 html 表单..

<form action="add.php" method="post">
<table style="border: 1px solid black;padding:20px;"cellspacing="1px">
</br></br>
<tr>
<td>Issue No:</td> <td><input name="issueno" type="text" id="issueno"/></td>
<td>Issue Date:</td> <td><input name="issuedate" type="date" id="issuedate"/></td></tr></br></br><tr></tr>
<tr>
<td>Item Code:</td> <td><input name="itemcode" type="text" id="itemcode" /></td>
<td>Description:</td> <td><input name="des" type="text" style="width:250px; height:23px" id="desc"/></td>
<td>Bal Quantity:</td> <td><input name="bal" type="text" id="bal"/></td>
</tr></br>
<tr> <td>Issue Quantity:</td> <td><input name="issuequ" type="text" id="issuequ"/></td></tr></br><tr></tr>
<tr><td>Remark:</td> <td><input name="remark" type="text" style="width:250px; height:23px" id="remark"/></td></tr></br>

<tr><td colspan="6"><center><input type="submit" value="submit"></center></td></tr>
</table>
</form>

当我警报(数据)时,我收到此 samsung20.00。其中 samsung 是描述,20.00 是 bal。我想将描述 desc id 分配给 bal id。那么我该怎么做呢??

最佳答案

在你的ajax.php文件中,你需要使用json_encode函数,这样你就可以在得到响应后解析它:

include "db.php";
$itemcode=$_POST['itemcode'];

$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
$json = array("Item_Desc" = > $row['Item_Desc'],
"Balance_Stock" => $row['Balance_Stock']
);
}
echo json_encode($json);

进行调整后,您可以对响应进行编码,您的ajax需要解析它。

示例:

<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();

$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
var obj = JSON.parse(data);
$("#desc").html(obj.Item_Desc);
$("#bal").html(obj.Balance_Stock);
}
});
});

});

非常简单。

希望对你有帮助

关于php - 为两个单独的 DIV 标签分配 AJAX 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com