1

I'm using jQuery autocomplete for the first time. I've created a simple data base that contains several user names and a simple search box to practice the autocomplete function that I've just read about. I've verified that my mysql queries are working within mysql. I also verified that I'm connected to the database. I still can not get it to auto suggest terms. I think (though not 100% sure) that I've isolated my problem to the way that i'm using the $_REQUEST['term'] command. So two questions: Am I using this command correctly, and is there something wrong with how I'm doing auto complete? Note: I'm aware that I haven't sanitized the input. I'm just practicing the autocomplete stuff

Additional info " If I take out the $_request and just query all the users, it will return an autocomplete with all of database users. This happens regardless of what key I type. This would lead me to believe that it's receiving the ajax request.

here's what my console shows

enter image description here

here is my actest.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AutoComplete Test</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">

  <script> jQuery(document).ready(function($){
    $('#username').autocomplete(
        {source:'suggest_username.php',
        minLength:2
        });
    });
</script>


</form>
</head>
<body>

<form action="actest.php" method="POST">
    Enter Username:
    <input type="text" id="username" />

    <br />
    <input type="submit" value="Search" />


</body>
</html>

Here is my suggest_username.php

<?php


// here we set the database information
$SERVER = 'localhost';
$USER = 'user';
$PASS = 'password';
$DATABASE = 'database';

// we are instantiating the mysqli database object
$db = new mysqli($SERVER, $USER, $PASS, $DATABASE);

// error check
if($db->connect_errno > 0){

      // report an error
die('Unable to connect to database [' . $db->connect_error . ']');
}



$rs = 'SELECT * FROM users WHERE user LIKE "($_REQUEST['term']) .'%'"';


$result = $db->query($rs);

$data = array();

    while ($row = $result->fetch_assoc()) 
    {
        $data[] = array(
            'label' => $row['user'] ,
            'value' => $row['user']
        );
    }


// jQuery wants JSON data
echo json_encode($data);
flush();
3
  • 1
    If you open your browsers console, can you verify the AJAX requests are being sent to the server? Are there any other errors in your console? Commented Jun 2, 2014 at 14:29
  • why did you add a </form> in "<head> </head>" ? and form tag in <body> doesn't end? Commented Jun 2, 2014 at 14:32
  • @mituw16 I opened the console, I have 0 errors. I'm not sure how to check that the requests are being sent but I'll read up on that. If I take out the request term and just query all the users, it will return all users as soon as I type any letter at all. This would lead me to believe that it's sending the ajax Commented Jun 2, 2014 at 14:42

2 Answers 2

3

I would suggest this line has a syntax error on it:

$rs = 'SELECT * FROM users WHERE user LIKE "($_REQUEST['term']) .'%'"';

looks like it should be something like this:

$rs = 'SELECT * FROM users WHERE user LIKE "(' . $_REQUEST['term'] . ')%"';

Not sure if you need the brackets either and you may want to make sure you make it safe so you can't get and sql injection attack

Sign up to request clarification or add additional context in comments.

1 Comment

This new syntax worked (without the brackets). thanks
0

if you are using google chrome then press F12 to open console. in the console window, click on network tab and press F5 to reload your page. now try to type something on your autocomplete field and look for suggest_username.php in the console. make sure the response status code is not 404.

2 Comments

This is not an answer, and really should be comment
Thanks, I did check that. It appears to be sending the ajax. I posted a picture of what my console shows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.