dthh5038 2016-08-20 08:47
浏览 41

Ajax实时搜索没有输出

Hello I am a beginner in php and jQuery so an early sorry if my question is stupid. I have been searching quite a lot on ajax live search but there is not much help on internet... So when I type something in my input box nothing comes out, so I thought it was a problem to connect to the database but that works fine. And now I don't really know what to do because my code sounds right (even though it's not :-) ). If someone could help me it would be great thank you.

Here are the codes: HTML:

<!DOCTYPE html>
<html>
<head>
    <title>live search test</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script scr="jquery.js"></script>

    <script type="text/javascript">
      functon getNames(value) {
        $.post("fetch.php",{partialName:value},function(data)
          $("#results").html(data);
        });
      }

    </script>

</head>
<body>
    <h1>LIVE SEARCH WITH AJAX TEST</h1>
    <input type="text" onkeyup="getNames(this.value)">
    <br>
    <div id="results">

    </div>
</body>
</html>    

PHP:

<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("smartphone")or die(mysql_error());


$partialName = $_POST['partialName'];

$names = mysql_query("SELECT name FROM smartphone WHERE name LIKE '%$partialName%'");
while ($name = mysql_fetch_array($names)) {
    echo"<div>".$name['name']."</div>";
}

?>
  • 写回答

1条回答 默认 最新

  • dqgg25493 2016-08-20 08:54
    关注

    I think if you check your browser console tool, you will see '$ is undefined'.

    Unless it was for this example, the following is incorrect:

    <script scr="jquery.js"></script>
    

    It should be:

    <script src="jquery.js"></script>
    

    Please also switch your insecure SQL query to the secure PDO bound parameters style, to avoid being exploited.

    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000