【发布时间】: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并希望它以某种方式神奇地得到从某处传递的正确值。回调传递了两个参数,如文档中所述,event和ui。在ui中,您可以找到用户从自动完成下拉菜单中选择的选项。
标签: javascript php jquery mysql ajax