0

I would like to use the jquery autocomplete "function" with a php file as the source. I don't get why it is not working. If I use data entered in a variable everything works fine. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.

My HTML:

<input id="moi" type="text"/>

My JS :

$(function() {
        $( "#moi" ).autocomplete({
            source: "php/search_loc.php",
            minLength: 2
        });
    });

My PHP:

<?php
header('Content-Type: text/html; charset=utf-8');
require("../inc/connect.inc.php");
mysql_set_charset('utf8'); 

$result = mysql_query("SELECT * FROM search_loc");
$row=mysql_fetch_assoc($result);

while($row=mysql_fetch_assoc($result)){
    echo $row['srl_loc'].'<br>';}

?>
2
  • 2
    I've never used the jQuery autocomplete plugin, but I'm fairly certain that it's not delimited by <br>. Commented Feb 15, 2012 at 16:11
  • And you have twice $row=mysql_fetch_assoc($result); in there. is this on purpose? Commented Feb 15, 2012 at 16:39

2 Answers 2

3

Your PHP script should return JSON data,

In the Jquery UI autocomplete doc

The datasource is a server-side script which returns JSON data,
Sign up to request clarification or add additional context in comments.

1 Comment

You could put "[" before <?php, "]" after ?> and separate your strings with a ",". Or use json_encode on an array where you put your values
0

Wrap it in a json_encode() function:

echo json_encode($row['srl_loc']).'<br />';

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.