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));
}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用被标记为工作的代码而我没有得到任何结果,它只是空的。 注意:如果单独运行,MySQL会返回结果,所以它不是一个sql的东西。 有什么建议吗?

HTML:

 &lt; head&gt; 
&lt; title&gt; TODO提供 title&lt; / title&gt; 
&lt; meta charset =“UTF-8”&gt; 
&lt; meta name =“viewport”content =“width = device-width,initial-scale = 1.0”&gt; 
&lt;  link rel =“stylesheet”href =“https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”&gt;  
&lt; script src =“https://code.jquery.com/jquery-1.12.4.js”&gt;&lt; / script&gt; 
&lt; script src =“https://code.jquery.com  /ui/1.12.1/jquery-ui.js"></script>
&lt; / head&gt; 
&lt; body&gt; 
 
名称:&lt; input id =“hint”/&gt;  
 
&lt; script type =“text / javascript”&gt; 
 
 $(“#hint”)。autocomplete({
 source:function(request,response){
 $ .ajax({\  n url:“getMedicineNames.php”,
 dataType:“jsonp”,
 data:{
 name:request 
},
 success:function(data){
 response(data); 
}  
}); 
},
 minLength:3 
}); 
 
&lt; / script&gt; 
&lt; / body&gt; 
   
 
 <  p>  PHP:  
 
 
  require'connect.inc.php'; 
 $ mysql = mys  qli_connect(“$ db_host”,“$ db_username”,“$ db_pass”,“$ db_name”); 
 $ name = $ _POST ['name']; 
if($ name!=“”){
 \  n $ sql =“SELECT MedicineName FROM medicinetypes WHERE MedicineNAme LIKE'%$ name%'”; 
 echo json_encode(mysqli_fetch_all(mysqli_query($ mysql,$ sql),MYSQLI_ASSOC)); 
} 
  <  / pre> 
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    点赞 打赏 评论
  • dongpin2969 2017-08-01 11:25

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

    If not working please add your screenshot of JSON response.

    点赞 打赏 评论
  • 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 */
    
    点赞 打赏 评论

相关推荐 更多相似问题