Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:53:36+00:00 2026-06-10T12:53:36+00:00

I have a javascript code that I am using for my autocomplete search feature

  • 0

I have a javascript code that I am using for my autocomplete search feature and it is working fine. But if you look at the code below, the data that the search feature is returning is hard coded and I want to get the data from MySQL using PHP.

Anyone can help me how to convert the code below to use PHP query to gather data MySQL then use the results and pass it to javascript? Thank you.

//<![CDATA[
var a1;
var a2;

function InitMonths() {
    a2.setOptions({ lookup: 'January,February,March,April,May,June,July,August,September,October,November,December'.split(',') });
}

function InitWeekdays() {
    a2.setOptions({ lookup: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(',') });
}

jQuery(function () {

    a1 = $('#query').autocomplete({
        width: 448,
        delimiter: /(,|;)\s*/,
        lookup: 'Andorra,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,Belarus,Belgium,Belize,Benin,Bhutan,Bolivia,Bosnia Herzegovina,Botswana,Brazil,Brunei,Bulgaria,Burkina,etc'.split(',')
    });

    a2 = $('#months').autocomplete({
        width: 448,
        delimiter: /(,|;)\s*/,
        lookup: 'January,February,March,April,May,etc'.split(',')
    });

    $('#navigation a').each(function () {
        $(this).click(function (e) {
            var element = $(this).attr('href');
            $('html').animate({ scrollTop: $(element).offset().top }, 300, null, function () { document.location = element; });
            e.preventDefault();
        });
    });

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:53:38+00:00Added an answer on June 10, 2026 at 12:53 pm

    New

    Based on this auto complete plugin http://www.devbridge.com/projects/autocomplete/jquery/

    Your JavasSript will need to look something like:

    //Start auto complete
    a1 = $("#query").autocomplete({
        serviceUrl:'search.php', //tell the script where to send requests
        width: 448, //set width
    
        //callback just to show it's working
        onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); } 
    });
    

    And your PHP (search.php) will need to be:

    ///
    /*** connect to your DB here ***/
    ///
    
    //retrieve the search term and strip and clean input
    $term = trim(strip_tags($_GET['query'])); 
    
    //try to make user input safer
    $term = mysqli_real_escape_string($term);
    
    //build a query on the database
    $qstring = "SELECT description as value,id FROM test WHERE description LIKE '%".$term."%'";
    
    //query the database for entries containing the term
    $result = mysql_query($qstring);
    
    //array to return
    $reply = array();
    $reply['query'] = $term;
    $reply['suggestions'] = array();
    $reply['data'] = array();
    
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
    {
        //Add this row to the reply
        $reply['suggestions'][]=htmlentities(stripslashes($row['value']));
        $reply['data'][]=(int)$row['id'];
    }
    
    //format the array into json data
    echo json_encode($reply);
    

    The plugin expects json like the below which this PHP should provide

    query:'Li',
    suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'],
    data:['LR','LY','LI','LT']
    

    Note I haven’t tested this but it should be fine!


    Old Answer

    See here: http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/

    First of all if you’re not using the jQuery autocomplete plugin (the one supported by jQuery as part of jQuery UI) set that up. http://jqueryui.com/demos/autocomplete/#remote

    You’re asking how to completely rework the system.

    For a start you’ll need to use Ajax to send the match string to the database via PHP as a proxy, then PHP will need to return the results and have the Javascript read them.

    So you’ll need to use (as the config):

    a1 = $("#query").autocomplete({
    source: "search.php"
    width: 448  
    });
    

    And something like (as your PHP)

    //connect to your database
    
    $term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends
    
    $qstring = "SELECT description as value,id FROM test WHERE description LIKE '%".$term."%'";
    $result = mysql_query($qstring);//query the database for entries containing the term
    
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
    {
            $row['value']=htmlentities(stripslashes($row['value']));
            $row['id']=(int)$row['id'];
            $row_set[] = $row;//build an array
    }
    echo json_encode($row_set);//format the array into json data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working Javascript code that generates an RDF/XML document using variables picked
I have this javascript code below that uses jquery, it is suppoed to be
I have simple JavaScript code that is using the Ajax API for fetching a
I have seen Javascript code that uses parenthesis immediately after a function's closing curly
I have a javascript code that have span tag and inside the span tag
I have some Javascript code that will programmatically register an COM interop assembly by
I have some JavaScript code that works in IE containing the following: myElement.innerText =
I have some javascript code that creates an img tag with a mouseover callback,
I have some Javascript code that creates 2 arrays: One for Product Category and
I have some javascript code that saves a cookie. However, if after saving the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.