PHP:创建自动建议的ajax [英] PHP: Create ajax auto-suggest

查看:76
本文介绍了PHP:创建自动建议的ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个非常简单的ajax自动建议,它可以从数据库中检索一些数据.

I would like to create a very simple ajax auto-suggest which it can retrieve some data from database.

您可以在这里看到:

index.php

<html>
<head>
<script type="text/javascript">
function suggest() {
    var txtSearch = document.getElementById('txtSearch').value;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject('MicrosoftXMLHTTP');
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('myDiv').innerHTML = xmlhttp.responseText;
        }
    }
    var target = 'include.inc.php?txtSearch=' + txtSearch;
    xmlhttp.open('GET', target, true);
    xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" id="txtSearch" onkeyup="suggest();"/>
<div id="myDiv"></div>
</body>
</html>

incldue.inc.php

<?php

require_once 'connect.inc.php';

if (isset($_GET['txtSearch'])) {
    $txtSearch = $_GET['txtSearch'];
    getSuggest($txtSearch);
}

function getSuggest($text) {

    $sqlCommand = "SELECT `SurName` FROM `person` WHERE `SurName` LIKE '%$text%'";

    $query = mysql_query($sqlCommand);

    $result_count = mysql_num_rows($query);

    while ($row = mysql_fetch_assoc($query)) {
        echo $row['SurName'].'<br />';
    }

?>

问题:

在第22行出现以下错误,但是我不知道为什么:

Get following error on line 22, But i have no idea why :

Parse error: syntax error, unexpected end of file in C:\wamp\www\PHP_Ajax_Autosuggest\include.inc.php on line 22

PS:

我没有提到 connect.inc.php 内容,因为它可以正常工作.

And i didn't mentioned connect.inc.php content, because it works fine.

任何帮助将不胜感激.

推荐答案

jothikannan提到了引号问题,这对我来说很有意义.不过,我认为您也忘记了结束getSuggest()函数.在文件中的?> 之前添加} .

jothikannan mentioned the quotation marks issue, which makes sense to me. I think you also forgot to end your getSuggest() function, though. Add a } before the ?> in your file.

这篇关于PHP:创建自动建议的ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆