gpt4 book ai didi

javascript - 使用 Ajax 如何将表单中的下拉菜单选择值与数据库中的记录相匹配,并获取关联的记录/行以预填充表单?

转载 作者:行者123 更新时间:2023-11-30 22:50:50 25 4
gpt4 key购买 nike

我有一个带有选项的下拉菜单...我需要将选项的值与数据库中列 (invoiceid) 下的值相匹配,然后获取相关的记录/表单数据行以预填充我的单击我的预填充按钮时形成。以下是我所得到的,我觉得即使那样我也很接近。我不知道我遗漏了什么或如何使这段代码工作。非常感谢并提前感谢任何人的帮助。

表格

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<form action="#">


<select name="dropdown-select" id="dropdown-select">
<option value="invoiceidselection1">invoiceidselection1</option>
<option value="invoiceidselection2">invoiceidselection2</option>
</select>

<button id="submit-id">Prefill Form</button>


<input id="location" name="location" required="" type="text">

<input onclick="change" id="q1" name="q1" value="4.99" checked="checked" type="radio">

<input name="subcheck" value="0" type="hidden">
<input id="subcheck" name="subcheck" value="1" onclick="return false" checked="" type="checkbox">Agree to Terms of Service


<button id="btn1" type="submit" name="Submit">Submit</button>


</form>




<script src="http://code.jquery.com/jquery.min.js"></script>

<script>
$(function(){


$('#submit-id').on('click', function(e){ // Things to do when 'Submit Id' button is clicked
var invoiceid = $('#dropdown-select').val(); // Grab user id from text field
e.preventDefault(); // Prevent form from submit, we are submiting form down with ajax.

$.ajax({
url: "orders.php",
data: {

}
}).done(function(data) {
data = JSON.parse(data);
$('#location').val(data.location);
$('#q1').val(data.q1);
$('#subcheck').val(data.subcheck);
});
});
});
</script>
</body>
</html>

订单.php

<?php

// Create the connection to the database
$con=mysqli_connect("xxx","xxx","xxx","xxx");


// Check if the connection failed
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}



if (isset($_GET['dropdown-select']))
{
$dropdown-select= $_GET['dropdown-select'];

$query = "SELECT location, q1, subcheck
FROM seguin_orders
WHERE invoiceid = '".($dropdown-select)."'";

$result = mysqli_query($con,$query);

while ($row = mysqli_fetch_assoc($result))c{
echo json_encode($row);
die();
}


?>

最佳答案

while ($row = mysqli_fetch_assoc($result))c{
echo json_encode($row);
die(); // assuming there is just one row
}

现在在 JavaScript 端

.done(function(data) {
data = JSON.parse(data);
$('#location').val(data.location);
$('#q1').val(data.q1);
$('#subcheck').val(data.subcheck);
});

我假设该表具有以下列 locationq1subcheck

编辑

您还需要在请求中发送获取参数。在 $.ajax 中。将 data 设置为:

    {
'dropdown-select': invoiceid
}

最好用$.get

$.get('orders.php', {
'dropdown-select': invoiceid
}).done(function (data) { .....});

如果还是不行.. 移除 data=JSON.parse(data)

希望它现在有效。

关于javascript - 使用 Ajax 如何将表单中的下拉菜单选择值与数据库中的记录相匹配,并获取关联的记录/行以预填充表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28174459/

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