user3144149
user3144149

Reputation: 21

Get results from database using AJAX

In line 10, I have insert getSuggestion(q); for me to get results from my database but it does not work.

What should I put there in order for me to retrieve results from database while other codes remain the same?

This is my current code:

<html>

    <script type="text/javascript" src="javascript/hogan-2.0.0.js"></script>
    <script type="text/javascript" src="javascript/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="javascript/typeahead.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('.q').typeahead({
                getSuggestion(q);
            });
        });
    </script>

    <script type="text/javascript">

        //document.getElementById("suggestion")

            function getSuggestion(q) {

            var ajax;
            if(window.XMLHttpRequest)//for ie7+, FF, Chrome
                ajax = new XMLHttpRequest();//ajax object
            else
                ajax = new ActiveXObject("Microsoft.XMLHTTP");//for ie6 and previous
            ajax.onreadystatechange = function() {
                if(ajax.status === 200 && ajax.readyState === 4) {
                    //if result are not there then don't display them
                    if(ajax.responseText === "")
                        document.getElementById("suggestion").style.visibility = "hidden";
                    else {
                        document.getElementById("suggestion").style.visibility = "visible";
                        document.getElementById("suggestion").innerHTML = ajax.responseText;
                    }
                }
            };
            ajax.open("GET", "suggestion.php?q=" + q, false);
            ajax.send();
        }
    </script>
</html>

Thanks in advance.

Upvotes: 0

Views: 901

Answers (1)

Rene Korss
Rene Korss

Reputation: 5484

Replace

<script type="text/javascript">
    $(document).ready(function() {
        $('.q').typeahead({
            getSuggestion(q);
        });
    });
</script>

with this:

<script type="text/javascript">
    $(document).ready(function() {
        $('.q').typeahead({
            name: 'q',
            remote: '/suggestion.php?q=%QUERY',
            minLength: 3, // start searching if word is at least 3 letters long. Reduces database queries count
            limit: 10 // show only first 10 results
        });
    });
</script>

And that's all you need. Typeahead does the rest.

Query is in $_GET['q']

You can find my example here

My index.php looks like this:

<html>
<head>
    <link rel="stylesheet" href="http://twitter.github.io/typeahead.js/css/main.css">
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function() {
        $('.q').typeahead({
            name: 'q',
            remote: '/typeahead/suggestion.php?q=%QUERY',
            minLength: 3, // start searching if word is at least 3 letters long. Reduces database queries count
            limit: 10 // show only first 10 results
        });
    });
    </script>
</head>
<body>
    <input type="text" class="q typeahead tt-query" />
</body>

You dont need css file or classes to input. Only class "q" is needed.

<input type="text" class="q" />

And suggestion.php source:

<?php
$q = $_GET['q'];
echo json_encode(array($q));
?>

As you can see, whetever you currently search, it answers with the same result as you typed. You have to make database query and echo array with json_encode

Remember to add correct url to remote parameter.

EDIT: My example now gets data from itunes and searches for music videos. Edited source available in example.

Upvotes: 1

Related Questions