xan
xan

Reputation: 4696

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

Upvotes: 0

Views: 502

Answers (1)

xan
xan

Reputation: 4696

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

Upvotes: 2

Related Questions