doutan8601
2017-08-01 11:09 阅读 127

使用JQuery自动完成与php / mysql无法正常工作

I am trying to use a code that was flagged as working and I am not getting any results, it's just empty. Notes: The MySQL does return results if run alone, so it's not an sql thing. Any suggestions?

HTML:

<head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">       
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    </head>
    <body>

        Name: <input id="hint" />

        <script type="text/javascript">

            $("#hint").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "getMedicineNames.php",
                        dataType: "jsonp",
                        data: {
                            name: request
                        },
                        success: function (data) {
                            response(data);
                        }
                    });
                },
                minLength: 3
            });

        </script>
    </body>

PHP:

require 'connect.inc.php';
$mysql = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");
$name = $_POST['name'];
if ($name != "") {

    $sql = "SELECT MedicineName FROM medicinetypes WHERE MedicineNAme LIKE '%$name%'";
    echo json_encode(mysqli_fetch_all(mysqli_query($mysql, $sql), MYSQLI_ASSOC));
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dptx8888 dptx8888 2017-08-01 12:22

    Alright I found a better answer, but the main problem was my php code where I left the array as an assoc which is wrong.

    JS:

    $(function () {
        $("#hint").autocomplete({
            source: 'getMedicineNames.php'
        });
    });
    

    PHP:

    $mysql = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");
    $name = $_GET['term'];
    
    $sql = "SELECT MedicineName FROM medicinetypes WHERE MedicineNAme LIKE '%$name%'";
    $res = mysqli_query($mysql, $sql);
    $res = mysqli_fetch_all($res, MYSQLI_ASSOC);
    
    foreach($res as $row){
        $data[]=$row['MedicineName'];
    
    }
    echo json_encode($data);
    
    点赞 评论 复制链接分享
  • dongpin2969 dongpin2969 2017-08-01 11:25

    Please add dataType: "json", & type : "GET".

    If not working please add your screenshot of JSON response.

    点赞 评论 复制链接分享
  • dshtze500055 dshtze500055 2017-08-01 11:52

    Bilal Ahmed answer is correct.

    Aditinoally this is a bit incorrect:

    $name = $_POST['name']; // Maybe cause a PHP Warning
    

    You can solve this creating a unset value:

    $name = isset($_POST['name']) ? $_POST['name'] : "";
    /* "" can be NULL, FALSE, 0 or any value */
    

    In PHP7 you can make this:

    $name = $_POST['name'] ?? "";
    /* "" can be NULL, FALSE, 0 or any value */
    
    点赞 评论 复制链接分享

相关推荐