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 9039023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:38:11+00:00 2026-06-16T09:38:11+00:00

I want to load the whole source data via jquery from the server but

  • 0

I want to load the whole source data via jquery from the server but only once on pageload. I want to store it in a variable. The jquery part works but the input does not autocomplete. It does nothing. It works only if the source is written like source: [“blablabla”,”dadadada”].

This is my Javascript Code:

var datasource;          // this is the variable where my source will be stored

$.post("typeahead.php",
  {
    query: 'query'       // 'query' has no meaning ;)
  }, 
  function(data) {       // data looks like ["asd","fds"] thanks to json_encode on the server side
    datasource = data;
});

$('#searchInput').typeahead( {
  source: datasource
});

Server Side php code:

    /* connect to the db */
    $con = mysql_connect("localhost","fahrschulesql1","******");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    // Select Database
    $db_selected = mysql_select_db("fahrschulesql1", $con);
    if (!$db_selected) {
        die ("Select DB error: " . mysql_error());
    }
    $query = "SELECT Vorname, Nachname FROM Benutzer b, Fahrlehrer f WHERE b.BenutzerID = f.BenutzerID";
    $result = mysql_query($query) or die ("MySQL-Error: " . mysql_error());
    while($row = mysql_fetch_array($result)){
        $array[] = $row["Vorname"] . " " . $row["Nachname"]; 
    }
    echo json_encode($array);
    mysql_close($con);

What am I doing wrong?

  • 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-16T09:38:12+00:00Added an answer on June 16, 2026 at 9:38 am

    You are losing the reference to the array datasource by assigning a new array. You will need to manipulate the array to avoid losing the reference to it.

    var datasource = [];
    
    $.post("typeahead.php", {
        query: 'query'
    }, function(data) {
        /* Add the responses to the datasource, don't mess up the reference */
        [].push.apply(datasource, data);
    });
    
    $('#searchInput').typeahead({
        source: datasource
    });
    

    See it here.


    Another option is caching the response. I personally prefer this method over the previous one.

    You can use the process callback after sending the first request and cache the data. Onwards, use the cached data.

    var cachedsource = (function(){
        var datasource = null;
        return function(query, process){
            if(datasource !== null) {
                /* use cached data */
                return datasource;
            } else {
                $.post("typeahead.php", {
                    query: 'query'
                }, function(data) {
                    /* cache data */
                    datasource = data;
                    process(datasource);
                });
            }
        };
    })();
    

    $('#searchInput').typeahead({
        source: cachedsource
    });
    

    See it here.


    PHP is returning incorrect Content-Type. Try $.ajax instead of $.post.

    $.ajax({
      url: "typeahead.php", 
      data: {
        query: 'query'
      },
      success: function(data) {
        /* cache data */
        datasource = data;
        process(datasource);
      },
      dataType: "json"
    });
    

    Notice the dataType is set to json.

    You can also set the correct Content-Type in PHP using header().

    header('Content-Type: application/json');
    echo json_encode($array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load data from MySql database to a HTTP form When I
I want to extract from a connectionString (a string variable) the server and database
I want to load the form fields using json data. I am using extjs
I want to load an image via css that stretches to the entire screen.
I want to load PNG-file from resources. There is a roughly MFC-way (with CResourceStream
I want to load a palette from a bitmap file I have created. The
I want to load defined folders into my website from same system, now I
So I use .load to show some information from php files that output data.
I want to use mysql LOAD DATA to import CSV file. Numbers in my
I have this error while using DiH for importing data from SQL Server to

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.