doutan8601 2017-08-01 11:09
浏览 128
已采纳

使用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 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?