0

I want to make autocomplete feature using jquery ui when searching data from mysql database....for nw am using data from array, but I dont know. How can I check it from the database? Here is my code.

<script>
$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];

    $( "#tags" ).autocomplete({
        source: result_array 
    });
});
</script> 

any help?

Edit to:

?php  
$q=mysql_query("select rfname from research_details") or die(mysql_error()); 

$array_data = mysql_fetch_array($q);

foreach($array_data as $data) {
 $data1[] = "'".$data."',";
}
$data1 = implode(",",$data1); 
$data = substr($data1,0,-1);


?>
$(function() {

var availableTags = [
<?php echo $data; ?>
];
$( "#tags" ).autocomplete({
source:availableTags 
});
});
</script>
0

6 Answers 6

1

Using AJAX (jQuery.ajax()) you can send a request to a PHP file on your server to retrive the data from the database and return it as a JSON encoded string which you'll then parse in jQuery / Javascript and add it to the DOM.

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

Comments

0

You can code it like the following:

$("#tags").autocomplete({
    source: function(request, response) {
        jQuery.ajax({
            url: '/autocomplete_query.php',
            dataType: 'json',
            success: function(data) {
                response(data);
            }
    });
});

The structure of the result in your SELECT query at autocomplete_query.php should be something similar to this array:

$result = array(0 => "ActionScript", 1 => "AppleScript", 2 => "Asp",...);

And then should be returned like this:

echo json_encode($result);
  • And make sure that that's the only echo statement on autocomplete_query.php.

2 Comments

tanx, this seems helpfull....but i don get wat u meant by this $result = array(0 => "ActionScript", 1 => "AppleScript", 2 => "Asp",...);
sorry for that! what i only meant by that was, the $result should just be a one-dimensional array and should not be multi.
0

In place of availableTags you need to get the result as json format and you can get those results using ajax.On the ajax call you need to get the data from Database and then encode them in JSON format like

echo json_encode($result_array);

and parse them and add that to the availableTags.Try these JSON ENCODE

Comments

0

Look at the jQueryUI API documentation for autocomplete.

Basically, you specify a URL as the source. That URL then outputs a page that jQuery can handle, such as JSON or XML data.

$('#autocomplete').autocomplete({
    source: '/data/my-page.php'
});

Comments

0

You need to study following links Link1 Link2 Link3 and Demo

1 Comment

hey thenx alot maryam for dat links, they are great
0

You can try like this. Get data from db as array, then pass it in you jquery function.

<?php
$array_data;  // get data from db as array
foreach($array_data as $data) {
$data1[] = "'".$data."',";
} 
$data1=implode(",",$data1);
   $data = substr($data1,0,-1);
?>

<script>
$(function() {

var availableTags = [
<?php echo $data; ?>
];
$( "#tags" ).autocomplete({
source:result_array 
});
});
</script> 

2 Comments

see my update, but i still get a warning ) Warning: substr() expects parameter 1 to be string, array given in
try with implode function before the substr finction

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.