javascript - 使用 javascript 在 mySQL 查询后填充表单

标签 javascript php jquery html mysql

我正在尝试做这件事:

我有一个 html 输入文本框、一些对数据库进行查询并返回 JSON 元素的 php 代码,最后还有一些我无法正常工作的 javascript。

我只想在用户键入时进行实时搜索,而不是选择从实时搜索中找到的一条记录并使用该记录的数据填充表单。

可能有一个非常简单的解决方案,但我是新手。

这是我的 html 和 Javascript 代码:

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8" />

    <title>InChiaro Ticket Admin</title>
    <meta name="description" content="The HTML5 Herald" />
    <meta name="author" content="SitePoint" />
    <link href="../assets/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="../assets/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" />
    <link href="../assets/bootstrap/css/bootstrap-fileupload.css" rel="stylesheet" />
    <link href="../assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <link href="css/style.css" rel="stylesheet" />
    <link href="css/style-responsive.css" rel="stylesheet" />
    <link href="css/style-default.css" rel="stylesheet" id="style_color" />
    <link href="../assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet" />
    <link href="../assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>

<body>
    <div id="main-content">
        <div class="wrapper-principale">
            <div class="input-append search-input-area">
                <input type="text" class="search-filter" id="searchcodiceCliente" name="codiceCliente" placeholder="Cerca un cliente..." /> <!-- text AREA CODICE CLIENTE-->
                <button class="btn" type="button"><i class="icon-search"></i> </button>
            </div>
            <div id="result" style="display:none">
                <table id="tablesearch"></table>
            </div>

            <form>
                <input type="text" id="CodiceCliente" />
                <input type="text" id="denominazione" />
            </form>
        </div>
    </div>
    <script type="text/javascript">
        $(function () {
            // We add the event on the class, which both inputs have
            $(".search-filter").keyup(function () {
                // Now we get the values from both inputs, using their ID's
                var codiceCliente = $("#searchcodiceCliente").val();
                //var fname = $("#searchfname").val();

                // Add both to the dataString (and URI encode the strings)

                var requestCodCliente = "get_codiceCliente_json"
                var json;
                // Check that at least one has any content
                if (codiceCliente != '')


                    $.ajax({
                        type: "POST",
                        url: "ajax_requests.php",
                        data: {
                            request: requestCodCliente,
                            searchCliente: codiceCliente
                        },
                        success: function (result) {
                            var x = document.getElementById("result");
                            x.style.display = "inline";
                            document.getElementById("tablesearch").innerHTML = '';
                            var th = "<tr><th></th><th>Codice Cliente</th><th>Denominazione</th><th>Indirizzo</th><th>Città</th><th>CAP</th><th>Numero Telefono</th></tr>";
                            document.getElementById("tablesearch").innerHTML += th;
                            function populateForm() {
                                document.getElementById("CodiceCliente").value = result[index].codiceCliente;
                            }
                            for (var index = 0; index < result.length; ++index) {

                                var t = "";

                                var tr = "<tr>";
                                tr += "<td><button id='select' class='btn'type='button' onclick='populateForm()'><i class='icon-search'></i></button></td>";
                                tr += "<td>"+result[index].codiceCliente+"</td>";
                                tr += "<td>"+result[index].denominazioneCliente+"</td>";
                                tr += "<td>"+result[index].indirizzo+"</td>";
                                tr += "<td>"+result[index].citta+"</td>";
                                tr += "<td>"+result[index].CAP+"</td>";
                                tr += "<td>"+result[index].numeroTelefono+"</td>";
                                tr += "</tr>";
                                t += tr;

                                document.getElementById("tablesearch").innerHTML += t;

                                }
                        }
                    });

            });
        });

    </script>
</body>
</html>

这是一些示例输出,我希望能解释我的意思:

Codice cliente denominazione 
c00106         Paolo re
c00116         viglino arturo 
c00126         sellaro giuseppe
c00146         accusani fabio 
c00161         franconi srl 

谢谢

最佳答案

您最困扰的方面是 populateForm 的附件作为点击处理程序。就目前情况而言,onclick='populateForm()不起作用,因为 populateForm需要成为全局成员,并且最好不要污染全局 namespace 。

为了克服这个问题,可以将点击处理委托(delegate)给按钮的祖先元素; <table> element 是最明显的选择。幸运的是,jQuery 有一个非常方便的事件委托(delegate)语法。

此外,还有一个问题您可能没有意识到;也就是说,多个快速 AJAX 请求不一定会按预期顺序响应。假设顺序很重要,可以使用一种简单的机制来确保表条目按预期顺序排列。您需要做的就是:

  • 当发出每个 AJAX 请求时,同步附加 <tbody>元素。
  • 保留对每个附加的 <tbody> 的引用元素(在闭包中)。
  • 收到每个 AJAX 响应后,将行附加到相应的 <tbody>元素。

你的代码应该是这样的:

$(function () {
    // Delegate handling of button.btn clicks to the containing table element. 
    // This avoids having to attach the same click handler repeatedly to buttons in dynamically generated lines.
    $("#tablesearch").on('click', 'button.btn', function() {
        $("#CodiceCliente").val($(this).closest('td').next('td').text());
    });

    $(".search-filter").keyup(function() {
        var codiceCliente = $(this).val();
        if (codiceCliente != '') {
            var $tbody = $('<tbody/>').appendTo("#tablesearch"); // Synchronously append a <tbody> to receive two asynchrously generated <tr>s (see below).
            $.ajax({
                'type': 'POST',
                'url': 'ajax_requests.php',
                'data': {
                    'request': 'get_codiceCliente_json',
                    'searchCliente': codiceCliente
                },
            }).then(function (result) {
                $("#result").css('display', "inline");
                $("<tr><th></th><th>Codice Cliente</th><th>Denominazione</th><th>Indirizzo</th><th>Città</th><th>CAP</th><th>Numero Telefono</th></tr>").appendTo($tbody); // append to the correct <tbody> for this response
                for (var i = 0; i < result.length; ++i) {
                    $("<tr><td><button class='btn'><i class='icon-search'></i></button></td><td>" + 
                    result[i].codiceCliente + "</td><td>" + 
                    result[i].denominazioneCliente + "</td><td>" + 
                    result[i].indirizzo + "</td><td>" + 
                    result[i].citta + "</td><td>" + 
                    result[i].CAP + "</td><td>" + 
                    result[i].numeroTelefono + "</td></tr>").appendTo($tbody); // append to the correct <tbody> for this response
                }
            }, function() {
                $tbody.remove(); // ajax failed so no point keeping the <tbody> element (unless you want to display an error message in the table)
            });
        }
    });
});

关于javascript - 使用 javascript 在 mySQL 查询后填充表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270284/

相关文章:

PHPUnit - 当 processIsolation 设置为 false 时无法重新声明类

javascript - jQuery ajax 混合 html/js &lt;script&gt; 避免在 &lt;script&gt; 打开时丢失上下文

javascript - 停止在 Dropzone 中上传文件

javascript - 我该如何才能使我的 API 只能通过我的应用程序(Javascript 和 PHP)访问?

javascript - 尝试在用户更改页面语言时翻译 NgPickDateTime 标签

javascript - API 路由在不应发生的情况下发生错误处理

php - 映射网络驱动器

php - header 在带有 JS 输出的 PHP 文件上过期

javascript - jQuery 选择器不起作用?

javascript - slider 图像上的淡入和淡出