简体   繁体   中英

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. The fields in the search box are searched from 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)

<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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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