【问题标题】:Using Ajax how can I match a input value from a form to records in my database and fetch associated record/row to prefill a form?使用 Ajax 如何将表单中的输入值与数据库中的记录匹配并获取关联的记录/行以预填充表单?
【发布时间】:2015-03-26 20:40:56
【问题描述】:

我有一个名为“invoiceidcopy”的表单输入...我需要将此输入值的值与我的数据库中列 (invoiceidcopy) 下的值匹配,然后获取关联的表单数据记录/行单击“#submit-id”按钮时预填写我的表单。我的表单和数据库具有以下输入/列,名为“invoiceidcopy”、“location”、“q1”、“subcheck”。以下是据我所知,即使这样,我也觉得我很接近。我无法弄清楚我缺少什么或如何使这段代码工作。非常感谢并提前感谢任何人的帮助。

表格

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

<form action="#">

<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>

/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(); 
} 


    ?>

【问题讨论】:

    标签: php jquery ajax forms mysqli


    【解决方案1】:

    您的表单操作似乎无效。至少应该是“/”。

    之后,我会检查服务器日志,看看是否有什么地方出了问题。您是否已验证查询直接在数据库上运行?

    【讨论】:

    • 是的,它只是一个占位符
    • 我一直在反复检查这段代码,但我无法弄清楚我错过了什么......
    • 就像我说的,检查你的服务器日志,看看是否有错误。如果没有,请使用 mysql 控制台检查查询是否实际返回任何内容。你到底看到了什么
    • 当我点击按钮时没有任何反应...控制台显示其返回值...但没有值被预填在表单输入中...感谢您的时间
    • 好吧,除了将 json_encode 的版本回显到页面上之外,您什么也没做。我没有看到与填充表单字段相关的代码。
    猜你喜欢
    • 2015-03-26
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2012-09-14
    • 1970-01-01
    相关资源
    最近更新 更多