【问题标题】:PHP: Create ajax auto-suggestPHP:创建 ajax 自动提示
【发布时间】:2013-08-10 16:09:27
【问题描述】:

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

你可以在这里看到:

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>

包括.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 行出现以下错误,但我不知道为什么:

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

附注:

我没有提到connect.inc.php 的内容,因为它运行良好。

任何帮助将不胜感激。

【问题讨论】:

  • 问题是......
  • 继续努力吧!

标签: php ajax


【解决方案1】:

您没有正确关闭 getSuggest($text) 函数。只需在 } 之前添加?>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 糟糕!我的错误:(谢谢
    【解决方案3】:

    你只是缺少一个右括号来关闭函数。在最后添加,你应该很好。最后三行应该是:

            }
        }
    ?>
    

    【讨论】:

      【解决方案4】:
      <?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 />';
      }
      } // You forgot this curly brace
      
      ?>
      

      它现在可以正常工作了.. :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-04
        • 1970-01-01
        • 2013-01-14
        • 2012-05-18
        • 2019-12-23
        • 1970-01-01
        • 2012-07-26
        • 1970-01-01
        相关资源
        最近更新 更多