【问题标题】:Ajax/PHP - autocomplete value not recognisedAjax/PHP - 无法识别自动完成值
【发布时间】:2017-08-30 11:30:11
【问题描述】:

我正在使用this example 从 mysql 数据库运行 ajax 数据查询,它返回一个表。

当手动将文本输入此输入表单时,这可以正常工作,例如:

但是搜索表单有一个自动完成的 jquery 脚本,可以帮助用户。从下拉自动完成值中选择一个值时,无法识别 onchange 事件,没有表格显示。

我的问题是,如何在搜索表单的末尾放置一个按钮,将其更改为“onclick”事件,而不是“onchange”?我面临的障碍是“client_address”的输入是较大表单的一部分,单击任何按钮上的提交都会导致页面尝试提交整个表单。

create_wt.php:

// autocomplete 
<script type="text/javascript">
    $(function() {
        $( "#clientsearch" ).autocomplete({
            source: 'backend_search_addressWT.php'
        });
    });
</script>
// retrieve data in table
<script>
    function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET","ajax_get_client_info.php?q="+str,true);
            xmlhttp.send();
        }
    }
</script>


<div class="form-group <?php echo (!empty($client_address_err)) ? 'has-error' : ''; ?>">
    <label>Address</label>
    <div class = "input-group">
        <input id="clientsearch" type="text" name="client_address" onchange="showUser(this.value)" class="input-group form-control" value="<?php echo $client_address; ?>" placeholder="Search by address..."  style="width: 500px;">
        <!---<span class="input-group-btn">
        <button class="btn btn-success" value="submit" id="ajaxbtn" type="submit">Get Client Info</button>
        </span> -->
    </div>
    <br><div id="txtHint"><b>Person info will be listed here...</b></div>
    <span class="help-block"><?php echo $client_address_err;?></span>
</div>

ajax_get_client_info.php:

<?php
    require_once 'config.php';
    $q = trim($_GET['q']);
    $query = $mysqli->query("SELECT * FROM client WHERE client_address LIKE '%".$q."%'");


    //$data = array();

    //while ($row = $query->fetch_assoc()) {
        // $data[] = ($row);
    //}

    //echo json_encode($data);

    echo "<table>
    <tr>
    <th>client_id</th>
    <th>Client Name</th>
    <th>Phone Number</th>
    </tr>";
    while($row = mysqli_fetch_array($query)) {
        echo "<tr>";
        echo "<td>" . $row['client_id'] . "</td>";
        echo "<td>" . $row['client_name'] . "</td>";
        echo "<td>" . $row['client_phone'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

?>

编辑:

create_wt:

<script type="text/javascript">
$(function() {
 $( "#clientsearch" ).autocomplete({
            select: function showUser(str) {
            if (str == "") {
                document.getElementById("txtHint").innerHTML = "";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                } else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("txtHint").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET","ajax_get_client_info.php?q="+str,true);
                xmlhttp.send();
            }
}
 }
 {
  source: 'backend_search_addressWT.php'
 });
});
</script>

【问题讨论】:

  • 我不会使用额外的按钮,而只是从用户选择一个值时触发的自动完成回调中调用您的 showUser 函数...api.jqueryui.com/autocomplete/#event-select
  • @CBroe 我添加了一个带有新版本 create_wt.php 的编辑,您建议从自动完成功能调用 Showuser - 但现在两者都不起作用 - 是否需要调整 ID 标签?
  • 我的意思是 call 回调中的函数,而不是移动其中的整个函数。 (当用户实际输入输入字段时,您仍然需要调用该函数,因此您需要它在处理函数“外部”可用。)此外,您不能只将参数命名为 str 并希望它以某种方式神奇地得到从某处传递的正确值。回调传递了两个参数,如文档中所述,eventui。在ui 中,您可以找到用户从自动完成下拉菜单中选择的选项。

标签: javascript php jquery mysql ajax


【解决方案1】:

根据 jQuery UI Autocomplete 文档: http://api.jqueryui.com/autocomplete/#event-select

自动完成输入的 select 事件不接受用户输入作为参数,但有另外 2 个:eventui

所以您正在尝试访问值:ui.item.value,即所选项目的值。

所以这一定是你的问题。

CBroe 在 cmets 中有相同的答案。

....
function getHint(value) {
  if ((value !== "") && (typeof value !== 'undefined')) {
    if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    } else {
      // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
      if ((this.readyState == 4) && (this.status == 200)) {
        document.getElementById("txtHint").innerHTML = this.responseText;
      }
    };
    xmlhttp.open("GET","ajax_get_client_info.php?q=" + value, true);
    xmlhttp.send();
    return true; // return true to accept the selection of the user
  } else {
    document.getElementById("txtHint").innerHTML = "";
    $(this).autocomplete( "close" ); // Close the autocomplete popup manual because by returning false negates the selection but it does not close the popup.
    return false; // return false to negate the selection of the user
  }
}
$( "#clientsearch" ).autocomplete({
    select: function showUser(event, ui) {
      return getHint(ui.item.value);
    },
    change: function showUser(event, ui) {
      return getHint(ui.item.value);
    },
    {
    source: 'http://www.example.com'
    }
 });
 ....

请务必阅读文档,以便从一开始就知道如何处理这种情况。 希望这会有所帮助。

【讨论】:

  • 请您介意提供一个示例脚本来说明它的外观吗?几乎是一个初学者,我不确定我会怎么做。
  • 谢谢!不幸的是,这不起作用。选择下拉菜单时,该值不会插入到输入中,表格也不会显示。 CBroe 建议不要在回调中移动整个函数?
  • 是的,创建一个全局命名函数并将代码移到那里。为更改事件做同样的事情。
【解决方案2】:

将输入框中的onchange改为onkeyup,应该可以解决,onchange主要用于单选按钮和复选框。

<input id="clientsearch" type="text" name="client_address" onkeyup="showUser(this.value)" class="input-group form-control" value="<?php echo $client_address; ?>" placeholder="Search by address..."  style="width: 500px;">

【讨论】:

    【解决方案3】:

    使用输入类型按钮:

    <input type="button" name="" id="">
    

    并使用其click 事件。输入类型不会提交表单。

    【讨论】:

      猜你喜欢
      • 2021-07-13
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多