简体   繁体   English

在php和mysql中自动完成文本框

[英]auto complete text box in php and mysql

i`m trying to make a auto complete text box in php. 我试图在PHP中制作一个自动完整的文本框。 here is my JavaScript code 这是我的JavaScript代码

<script src="assets/js/jquery-1.10.2.js"></script>
<script src="assets/js/jquery-1.3.2.min.js"></script>
<script src="assets/js/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$('#search').autocomplete({
source : 'search-job.php',
minLength:2
}); 
});
</script>

here is my html code 这是我的HTML代码

<input type="text" id="search" class="form-control input-lg col-xs-4" name="search">

here is my search-job.php 这是我的search-job.php

<?php
include 'connectdb.php';
if(isset($_REQUEST['term']))
exit ();
$rs=  mysql_query("select * from jobs where `title` like '%"  . ucfirst($_REQUEST['term']). "%' ORDER BY id ASC LIMIT 0,10") or      die(mysql_error());
$data=  array();
while($row=  mysql_fetch_assoc($rs,MYSQL_ASSOC)){
$data[]=array(
'label'=>$row['title'],
'value'=>$row['title']
);
}
echo json_encode($data);
flush();

i`m getting correct values in search-job.php. 我在search-job.php中获得了正确的值。 but it is not catching by html but when i run code nothing happened? 但它没有被html捕获但是当我运行代码时没有发生任何事情? why is that? 这是为什么?

this is how it`s looks on network tab 这就是它在网络选项卡上的外观 根据这一点,它没有正确地传递值到术语

The array format you are passing is wrong. 您传递的数组格式是错误的。

$data[]=array(
'label'=>$row['title'],
'value'=>$row['title']
);

should be 应该

$data[$row['title']]= >$row['title'];
OR simply
$data[]= >$row['title'];

and

replace 更换

source : 'search-job.php',

with

source: 'search-job.php?term='+document.getElementById('search').value,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM