Connect autoSuggest with my databse.

Jonas's Avatar

Jonas

21 Nov, 2012 11:52 AM

Now, here is what i got in my body tag, i connect to the database fine, and i can echo my rows from my db, but i can't figure out how to connect my query with the json array so it can echo my tables form my DB in the autuSuggest input.

Can anyone tell me how i do this, would really appreciate it!

<?

$input = $_GET["q"];
$data = array();
// query your DataBase here looking for a match to $input
$query = mysql_query("SELECT * FROM hent WHERE navne LIKE '%$input%'");
while ($row = mysql_fetch_assoc($query)) {
    $json = array();
    $json['value'] = $row['id'];
    $json['name'] = $row['navne'];
    $data[] = $json;
    }

?>

var data = {items: [
    {value: "1", name: "Jacob"},
]};

$("input[type=text]").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});

});

    </script>
    <input type="text" name="name" id="name" />
  1. 1 Posted by Jon on 21 Nov, 2012 03:20 PM

    Jon's Avatar

    You are using:

    {items: [  {value: "1", name: "Jacob"}]};
    

    as your data. You have to echo your php variable in the javascript code;

  2. 2 Posted by Jonas on 21 Nov, 2012 06:29 PM

    Jonas's Avatar

    Alright i see, im pretty new at javascript ( recently started getting into it ), so im not quite sure how i echo my php variable to my javascript code, if you or someone could show me a quick example?

    & Thanks for your reply btw.

  3. 3 Posted by Jon on 21 Nov, 2012 08:40 PM

    Jon's Avatar

    It's the same as if you print/echo a php variable in HTML.

  4. 4 Posted by Jonas on 22 Nov, 2012 09:16 AM

    Jonas's Avatar

    Got it figured out thanks!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • index.php 2.83 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac