dtpoius74857 2018-08-28 09:28
浏览 57

自动完成功能不起作用...(AJAX + PHP + HTML + JS)

I've created autocomplete function but it doesn't work. I see no error and can't find out what's wrong...

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">

    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Metamorphous">
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

    <script src ="scripts.js"></script>
</head>
<body>
    <div id="container">
        <div id="main" class="down">
                <header>
                    <h1>HerbariuM</h1>
                </header>
                <nav>
                    <input id="searchBar" type="text" placeholder="Szukaj..." onfocus="mainUp()" onblur="mainDown()">
                </nav>
        </div>
        <div id="herb" class="hide">
            <img id="image" src="">
            <h2><i>Losowa roślinka</i></h2>
        </div>
    </div>
</body>
</html>

Here's just a fragment of code for autocomplete JS:

$(document).ready(function(){
$("#searchBar").autocomplete({
    source: function(request,response){
        $.ajax({
            url: "search.php",
            dataType:"json",
            data:{q:request.term},
            success: function(data){
                response(data);
            }
        });
    },
    minLength: 2,
    select: function(event,ui){
        alert("Selected: "+ui.item.label);
    }
});
});

And this is PHP code to return data from database PHP:

<?php

$connection = new mysqli("localhost","root","password","herbarium");

$herb = $_GET['q'];

$result = $connection->query("SELECT * FROM herbs WHERE latin_name LIKE '%$herb%'");

$data = array();

while ($row=$result->fetch_assoc()){
    $data[] = $row['latin_name'];
}

echo json_encode($data);

?>

Could anyone advice me how to fix it? Got no idea, have searched whole internet and all lines of code one by one but see no error...

  • 写回答

1条回答 默认 最新

  • dtxq82489 2018-08-28 11:00
    关注

    Fixed... The problem was using 'localhost' instead of '127.0.0.1' on my Mac. Came on it accidently, while trying to create new .php file and access it by webbrowser.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度