简体   繁体   English

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

[英]Autocomplete doesn't work.... (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: 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:这只是自动完成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 返回数据的 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...不知道,已经搜索了整个互联网和所有代码行,但没有看到任何错误......

Fixed... The problem was using 'localhost' instead of '127.0.0.1' on my Mac.已修复...问题是在我的 Mac 上使用“localhost”而不是“127.0.0.1”。 Came on it accidently, while trying to create new.php file and access it by webbrowser.在尝试创建 new.php 文件并通过网络浏览器访问它时意外出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM