gpt4 book ai didi

php - 使用ajax发布多个值

转载 作者:行者123 更新时间:2023-12-01 04:08:49 27 4
gpt4 key购买 nike

如何将数据库中的所有信息发布到指定的文本框中。我需要将值发布到文本框,例如,将 Pr # 数据发布到 pr_num 文本框等。问题是我的 ajax 函数仅适用于一个文本框。我如何将其发布到每个文本框中?任何帮助将不胜感激。

表结构

Pr #  | Supplier  | Receipt #  | Receiver  |
--------------------------------------------
321-B | Villman | 312312331 | John |
556-B | Dockers | 903232317 | William |

Ajax.php

<?php
if(isset($_POST['pr_code'])) {
$pr_code= $_POST['pr_code'];

$sql = $mysqli->query("SELECT * FROM pr_table WHERE pr='$pr_code'");

while($row = $sql->fetch_assoc())
{
$pr= $row['pr'];
$supplier = $row['supplier'];
$receipt_num= $row['receipt_num'];
$receiver= $row['receiver'];
}
echo $pr;
echo $supplier;
echo $receipt_num;
echo $receiver;
}
?>

index.php

<select id="pr">
<?php ... ?>
</select>

<input id="pr_num">
<input id="supplier">
<input id="receipt">
<input id="receiver">

<script type="text/javascript">
$(document).ready(function()
{
$('input[id="pr"]').change(function()
{
var prjt_code = $("#pr").val();
$.ajax({
type: "POST",
url: "ajax.php",
data :"pr_code="+pr_code,
dataType:'html',
type:'POST',
success:function(data){
$('#pr_num').val(data);
}
});
return false;
});
});
</script>

最佳答案

也获取其他文本框值并按如下方式发布

var pr_num= $("#pr_num").val();
var supplier= $("#supplier").val();
var receipt= $("#receipt").val();
var receiver= $("#receiver").val();

以及在ajax中

data :{"pr_code":pr_code,"supplier":supplier,"receipt":receipt_num,"receiver":receiver}
<小时/>

更新

在 php 中这样做

echo json_encode(array("pr" => $pr, "supplier" => $supplier,"receipt_num"=>$receipt_num,"receiver"=>$receiver)); 

在ajax中

获取像

这样的值
var pr=data.pr;
var supplier=data.supplier;
var receipt_num=receipt_num;
var receiver=receiver;
<小时/>

更新2

you have to add another option value,so that the onchange event will fired. If you have only one value then the change event will not be called.So add another option.

为什么要在选项标签之外打印 echo $option;

<select id="tag">
<option value="">wala</option><?php echo $option; ?>//what are you trying to do here
</select>

关于php - 使用ajax发布多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728752/

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