Jquery自动完成+ php + db + ajax

时间:2017-06-14 13:09:03

标签: php jquery ajax autocomplete jquery-ui-autocomplete

在我的项目中,我遇到了从数据库中恢复数据并将其发送到脚本的问题。

主脚本文件

<link rel="stylesheet" href="css/jquery-ui.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery-ui.min.js"></script>

...

<input type="text" class="form-control" name="startstation" placeholder="Stacja początkowa">

...

    $( "input[name=startstation]" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url : 'station.php',
                dataType: "json",
                data: {
                   q: request.term
                },
                 success: function( data ) {
                     response( $.map( data, function( item ) {
                        return {
                            label: item.name,
                            value: item.id
                        }
                    }));
                }
            });
        },
        minLength: 2 
    });

station.php

<?php
$connection = mysql_connect("localhost", "kilometry", "kilometry"); // Establishing Connection with Server..
$db = mysql_select_db("kilometry", $connection); // Selecting Database
//Fetching Values from URL
 $q=$_GET["q"]; 
 $sql="SELECT id, name FROM stations WHERE name LIKE '".$q."%' ORDER BY priority DESC";


 $result = mysql_query($sql);


$options = array();
while ($row_id = mysql_fetch_array($result)) {
    // more structure in data allows an easier processing
    $options['myData'][] = array(
        'name' => $row_id['name'],
        'id'    => $row_id['id']
    ); 
}
mysql_close($connection);
echo json_encode($options);
?>

在开发工具脚本中发送Q值作为参数但在回答字段中我没有回复:http://prntscr.com/fjo1gf

有什么问题?我认为这是条件失败。

0 个答案:

没有答案