1

When I do console.log(req.responsetext) i get [11:38:04.967] ReferenceError: req is not defined. But i define req as a new xml request on window load so I am kind of stumped. Is there a way that I should be passing a reference?

the console output is as follows

[12:29:06.839] GET getterms.php?query=DFA [HTTP/1.1 200 OK 99ms]
[12:29:06.888] SyntaxError: JSON.parse: unexpected character @ search.php:21
[12:33:24.316] console.log(req.responsetext)
[12:33:24.318] ReferenceError: req is not defined

Any and all help would be most gratefully appreciated. Thank you to anyone who takes the time to read and/or answer even if you cannot help!

    <!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <title>Auto Complete</title>
</head>

<body>
    <script>
        window.onload = function () {
            var req = new XMLHttpRequest();     //the HTTP request which will invoke the query
            var input = document.getElementById('search');      //where to grab the search from
            var output = document.getElementById('results');    //where to display the sugestions

            input.oninput = getSuggestions;

            function getSuggestions() {
                req.onreadystatechange = function () {
                    output.innerHTML = "";  //CLEAR the previous results!! only once the server can process new ones though
                    if (this.readyState == 4 && input.value != "") {
                        var response = JSON.parse(req.responseText);
                        for (var i = 0; i < response.length; i++)
                            addSuggestion(response[i].terms);
                    }
                }               
                req.open('GET', 'getterms.php?query=' + input.value, true); //GET request to getterms.php?=
                req.send(null);
            }

            addSuggestion = function (suggestion) {
                var div = document.createElement('div');
                var p = document.createElement('p');
                div.classList.add('suggestion');        //suggestion[x]...
                p.textContent = suggestion;             
                div.appendChild(p);
                output.appendChild(div);

                div.onclick = function() {
                    input.value = p.innerHTML;  //set the search box
                    getSuggestions();           //GET new suggesions
                }


            }
        }
    </script>

    <input type='text' id='search' name='search' autofocus='autofocus'>
    <div id='results'></div>
</body>
</html>

edit this is my php page that echos the json.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

if (!isset($_GET['query']) || empty($_GET['query']))
    header('HTTP/1.0 400 Bad Request', true, 400);
else {


    $db = new PDO(
    my database
    );


    $search_query = $db->prepare("
        SELECT * FROM `words` WHERE `word` LIKE :keywords LIMIT 5
    ");

    $params = array(
        ':keywords' => $_GET['query'] . '%',
    );

    $search_query->execute($params);
    $results = $search_query->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($results);
}
?>
4

1 回答 1

0

Scope problem! Remove var in front of req to make it global and it should work

于 2013-05-07T19:39:07.687 回答