Php 如何使用表单输入中的值在数据库中进行匹配,并获取记录/行数据以预填充表单?
我有一个名为“invoiceidcopy”的表单输入…我需要将此输入值的值与数据库中列(invoiceidcopy)下的值相匹配,然后在我点击“#提交id”按钮时获取其相关记录/表单数据行以预填充我的表单 我的表单和数据库有以下输入/列,名为“invoiceidcopy”、“location”、“q1”、“subcheck” 下面是我所能得到的,我觉得我已经很接近了 非常感谢任何人的帮助 表格Php 如何使用表单输入中的值在数据库中进行匹配,并获取记录/行数据以预填充表单?,php,arrays,json,forms,mysqli,Php,Arrays,Json,Forms,Mysqli,我有一个名为“invoiceidcopy”的表单输入…我需要将此输入值的值与数据库中列(invoiceidcopy)下的值相匹配,然后在我点击“#提交id”按钮时获取其相关记录/表单数据行以预填充我的表单 我的表单和数据库有以下输入/列,名为“invoiceidcopy”、“location”、“q1”、“subcheck” 下面是我所能得到的,我觉得我已经很接近了 非常感谢任何人的帮助 表格 <!DOCTYPE html> <html lang="en"> <he
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="/xxx.php">
<input id="invoiceidcopy" name="invoiceidcopy" type="text" value="XXXIDXXX"/>
<button id="submit-id">Prefill Form</button>
<input id="location" name="location" required="" type="text">
<input type="radio" id="q1" name="q1" value="4.99" checked="checked">
<input type="radio" id="q1" name="q1" value="7.99" />
<input id="subcheck" 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 invoiceidcopy = $('#invoiceidcopy').val(); // Grab user invoiceidcopy from text field
e.preventDefault(); // Prevent form from submit, we are submiting form down with ajax.
$.ajax({
url: "/tst/orders2.php",
data: {
'invoiceidcopy':invoiceidcopy
}
})
.done(function(data) {
data = JSON.parse(data);
$('#location').val(data.location);
$('#q1').val(data.q1);
$('#subcheck').val(data.subcheck);
});
});
});
</script>
</body>
</html>
文件
预填充形式
同意服务条款
提交
$(函数(){
$(“#提交id”)。在('click',函数(e){//单击“#提交id”按钮时要执行的操作
var invoiceidcopy=$('#invoiceidcopy').val();//从文本字段抓取用户invoiceidcopy
e、 preventDefault();//防止表单提交,我们正在使用ajax向下提交表单。
$.ajax({
url:“/tst/orders2.php”,
数据:{
“invoiceidcopy”:invoiceidcopy
}
})
.完成(功能(数据){
data=JSON.parse(数据);
$('#location').val(data.location);
$('#q1').val(data.q1);
$('#subcheck').val(data.subcheck);
});
});
});
/tst/orders2.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['invoiceidcopy']))
{
$invoiceidcopy= $_GET['invoiceidcopy'];
$query = "SELECT location, q1, subcheck
FROM seguin_orders
WHERE invoiceidcopy = '".($invoiceidcopy)."'";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)){
echo json_encode($row);
die();
}
?>
您的代码可以很好地用于文本字段和收音机/复选框。。。
但是.val()
将设置收音机/复选框的值
要在选中和未选中之间切换…等…您可能需要使用.attr()
或.prop()
并根据预先填充的值根据需要设置选中的
$("#q1").attr('checked', true);
$('#subcheck').prop('checked', true);
在打开之后,使用jQuery的Onclick事件将错误报告添加到文件的顶部,并使用AJAX将数据填充到表单中。