简体   繁体   English

PHP-SQl自动完成搜索

[英]PHP-SQl Autocomplete search

I've writing a code to log in a user and display his various details. 我编写了一个代码来登录用户并显示他的各种细节。 This is working perfectly fine. 这工作得非常好。 On addition to it; 除此之外; I added a autocomplete search that doesn't seem to work. 我添加了一个似乎不起作用的自动完成搜索。 I'm using Jquery's autocomplete. 我正在使用Jquery的自动完成功能。 The fields in the search box are searched from SQL. 搜索框中的字段将从SQL中搜索。

But nothing happens. 但没有任何反应。 I type in the text in the text-field but nothing happens. 我在文本字段中键入文本但没有任何反应。

Here's my whole corrected PHP code (excluding the connection file) 这是我整个更正的PHP代码(不包括连接文件)

<script>
$(document).ready(function() {
  $("input#autocomplete").autocomplete({
    source: keywordList,
    minLength: 1,

  });
});
</script>

<?php echo keywordArray(); ?>
<?php function keywordArray()
{
  $rsKeywords = mysql_query("SELECT * FROM job");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['work'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?> 

//search script: //搜索脚本:

<?php
if(!isset($_POST['submit'])){
                        echo "Your search was invalid";
                        exit;
                    } 

                    $keyword = mysql_real_escape_string($_POST['keywords']);
                    $sql = "SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' LIMIT 5";

                    $result = mysql_query($sql);
                    $numrows = mysql_num_rows($result);

                    echo //details etc
>?

Here's my whole corrected PHP code (excluding the connection file) 这是我整个更正的PHP代码(不包括连接文件)

<script>
$(document).ready(function() {
  $("input#autocomplete").autocomplete({
    source: keywordList,
    minLength: 1,

  });
});
</script>

<?php echo keywordArray(); ?>
<?php function keywordArray()
{
  $rsKeywords = mysql_query("SELECT * FROM job");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['work'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?> 

//search script: //搜索脚本:

<?php
if(!isset($_POST['submit'])){
                        echo "Your search was invalid";
                        exit;
                    } 

                    $keyword = mysql_real_escape_string($_POST['keywords']);
                    $sql = "SELECT * FROM job WHERE work='$keyword' or work LIKE 'ANOTHER_PARAMETER' LIMIT 5";

                    $result = mysql_query($sql);
                    $numrows = mysql_num_rows($result);

                    echo //details etc
>?

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

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