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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试