3

search.php

$text = $mysqli->$_POST['term'];
$query = "SELECT name FROM males WHERE name LIKE '%" . $text . "%' ORDER BY name ASC";
$result = $mysqli->query($query);
$json = '[';
$first = true;
while($row = $result->fetch_assoc())
{
    if (!$first) { $json .=  ','; } else { $first = false; }
    $json .= '{"value":"'.$row['name'].'"}';
}
$json .= ']';
echo $json;

index.php

1) HTML

<body>
Text: <input type="text" id="autocomplete" />
</body>

2) jQuery

    $( "#autocomplete" ).autocomplete({
        source: function(request, response) {
            $.ajax({ url: "http://localhost/testing/auto/search.php",
            data: { term: $("#autocomplete").val()},
            dataType: "json",
            type: "POST",
            success: function(data){
                response(data);
            }
        });
    },
    minLength: 2
    });

When I type 2 letters, it gives me all the names in my database even if these two letters do not match any of the names.

How does that happen and how do I fix it?

6
  • 2
    how does your json looks like? you should use json_encode function to produce json in php Commented Jun 10, 2011 at 6:57
  • still gives me all records after i added json_encode! Commented Jun 10, 2011 at 7:11
  • No, $text = $mysqli->$_POST['term']; Commented Jun 10, 2011 at 7:21
  • 1
    What does $mysqli->$_POST['term'] do? I think you should have $text = $_POST['term'];. This should work Commented Jun 10, 2011 at 7:34
  • @Rakesh , thank you its worked now without this "$mysqli->" Commented Jun 10, 2011 at 7:37

6 Answers 6

1

Looks like my comment worked as an answer, hence this answer.

What does $mysqli->$_POST['term'] do? I think you should have $text = $_POST['term'];. This should work.

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

Comments

1

Change the PHP to

$text = $_POST['term'];

$query = "SELECT name FROM males WHERE name LIKE '%" . $mysqli->real_escape_string($text) . "%' ORDER BY name ASC";
$result = $mysqli->query($query);

echo json_encode($result->fetch_all(MYSQLI_ASSOC));

You forgot to escape the input to prevent SQL injections. Additionally, use @mu's answer to fix your front-end code.

3 Comments

Nice, I didn't even notice the $text mistake until Swell pointed it out to me.
@mu And I didn't spot the JS errors at first. Guess we focus on different things ;-)
To be fair, the JS issues are more stylistic and about using the API as intended than "real" errors.
1

Your source callback isn't doing what you think it is. The current contents of the autocompleter are in request.term, not in $("#autocomplete").val(). You're sending an empty value for term back to your PHP and then your SQL looks like:

SELECT name FROM males WHERE name LIKE '%%' ORDER BY name ASC

which of courses matches everything in your males table. Your source callback should look like this:

source: function(request, response) { 
    $.ajax({
        url: "http://localhost/testing/auto/search.php",
        data: { term: request.term },
        dataType: "json", 
        type: "POST", 
        success: function(data) { 
            response(data);  
        }
    });
}   

The jQuery-UI widget probably manipulates the DOM a fair bit so the #autocomplete input element may not have anything useful in it until the autocompleter has a final value for it.

For reference, here's what the fine manual has to say:

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

  • A request object, with a single property called term, which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo".

4 Comments

What does $text look like in your PHP?
its worked also with data: { term: $("#autocomplete").val()},, the problem was $mysqli->$_POST['term'].
@Swell: Really? I didn't even notice that. So Znarkus gets the checkmark then. Even so, you should use request.term as that's the specified way and it saves you from all the extra work that $().val() has to do behind the scenes, it is probably more future proof as well.
thank you so much i would like to accept your answer but your answer not the right one regarding this issue, also i take your advice and i replaced $().val() with request.term , thank you again.
0

First make sure that your search.php is working. Switch to $_GET['term'] and run the page directly until you get the data you wanted.

I would personally change the query from from LIKE '%text%' to LIKE 'text%'.

After that use Firebug to examine the parameters that are transferred to the search.php with an AJAX call. BTW, are you sure that your jquery code is correct?

Comments

0

I think is your sql query. U were using

$query = "SELECT name FROM males WHERE name LIKE '%$text%' ORDER BY name ASC"

if your name list like

Aaron Alexander Maartha ...

If you type 'AA', the above query will result : Aaron, Maartha. Because the query will search matching given string between the whole strings. % text % means the query result will ignore the left and right result.

if you're looking for names field data you supposed to use query

$query = "SELECT name FROM males WHERE name LIKE '$text%' ORDER BY name ASC"

If you type 'AA, it will result : Aaron.

Note that jQuery only returns what you're asking to the database.

Comments

0

Use $_REQUEST['term'] instead $_POST['term'].

Comments

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.