javascript - how to connect jquery ui auto-complete widget with database?

190

upload.php

<?php
include('lib.php');

$text = $mysqli->real_escape_string($_GET['term']);
$query = "SELECT * FROM user WHERE area LIKE (:keyword) ORDER BY id ASC 
LIMIT 0, 10";
$result = $mysqli->query($query);
$json = '[';
$first = true;
while($row = $result->fetch_assoc()) 
{
if (!$first) { $json .=  ','; } else { $first = false; }
$json .= '{"value":"'.$row['area'].'"}';
 }
 $json .= ']';
 echo $json;
 ?>

area_list.js

$(function(){

   var test=["red","blue","pink","Black" ,"Grey"];  


   $("#term2").autocomplete({

    source:'<?php include upload_where.php; ?>'


    });


 });

HTML

<input id="term2" placeholder="e.g New Delhi, Mumbai" />

If you think the code is right is it possible maybe my server doesn't support json because i haven't used json before.

719

Answer

Solution:

You can't include your php in javascript. So you need to create a php script for autocomplete. In this script, its better using an array to store your results then convert it to json using "json_encode".

autocomplete.php :

<?php
include('lib.php');

if(true === isset($_GET['term']) && false === empty($_GET['term']))
{
    $text = $mysqli->real_escape_string($_GET['term']);
    $query = "SELECT * FROM user WHERE area LIKE (:keyword) ORDER BY id ASC 
    LIMIT 0, 10";
    $result = $mysqli->query($query);

    $json = array();
    $first = true;
    while($row = $result->fetch_assoc()) 
    {
        $json[] = $row['area'];
    }
    header('Content-Type: application/json');
    echo json_encode($json);
}
?>

For the javascript, use ajax to retrieve your results

$('#term2').autocomplete({    
        minChars:2,
        noCache: false, //default is false, set to true to disable caching    
        // callback functions:
        source: function( request, response ) {
            $.ajax({
                url: "autocomplete.php", //Correspond to PHP page
                dataType: "json",
                data: {term: request.term},
                success: function(data) {
                            return data;
                }
            });
        }
    });
51

Answer

Solution:

You were using a string as input but you do need an array as autocomplete source.

If you don't want to code an ajax request, and if your js is generated on the fly by php, you can replace your code by this one:

$(function(){
   var test=["red","blue","pink","Black" ,"Grey"];  
   $("#term2").autocomplete({
    source:<?php include upload_where.php; ?>
    });
 });

However it's not a good idea to use php to generate your js. So you can use ThinkTank answer's

People are also looking for solutions to the problem: php - Display email in a web page

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.