weixin_33724659 2013-08-10 16:09 采纳率: 0%
浏览 15

PHP:创建自动建议的ajax

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

You can see here :

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 />';
    }

?>

The problem :

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

P.S :

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

Any help would be great appreciated.

  • 写回答

4条回答 默认 最新

  • weixin_33690367 2013-08-10 16:17
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退