0

我编写了一个代码来登录用户并显示他的各种详细信息。这工作得很好。除此之外; 我添加了一个似乎不起作用的自动完成搜索。我正在使用 Jquery 的自动完成功能。搜索框中的字段是从 SQL 中搜索的。

但什么也没有发生。我在文本字段中输入文本,但没有任何反应。

这是我整个更正的 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;
}
?> 

//搜索脚本:

<?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
>?
4

1 回答 1

2

这是我整个更正的 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;
}
?> 

//搜索脚本:

<?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
>?
于 2012-06-22T16:51:48.717 回答