【发布时间】:2015-08-15 16:49:49
【问题描述】:
我正在尝试绑定我的 select2 输入以使用 mysqli 连接从 mysql 数据库中获取结果。我在这里尝试了几种解决方案,但尚未使其工作,而是一直说找不到结果。
我最近尝试的是来自https://select2.github.io/examples.html 的javascript 代码。我不确定是我的 javascript 还是我的 php 文件失败了。
这些是我的代码,希望有人能指出我需要更改的地方。
HTML
<div class="form-group">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label class="control-label col-lg-3" for="Customer" id="Customer"><span style="color:red;">*</span>Customer:</label>
<div class="col-lg-9">
<input id="cCustomer" name="cCustomer" class="cCustomer form-control" type="hidden" value="" style="width: 100%" />
</div><!-- END col-lg-9 -->
</div><!-- END col-xs-12 col-sm-12 col-md-6 col-lg-6 -->
</div><!-- END .form-group -->
我确实包括了
<link rel="stylesheet" href="//localhost/1System/select2/css/select2.min.css">
<link rel="stylesheet" href="//localhost/1System/select2/css/select2-bootstrap.min.css">
<script type="text/javascript" src="//localhost/1System/select2/js/select2.full.min.js"></script>
在我的<head></head>
JS
$(document).ready(function() {
$("#cCustomer").select2({
ajax: {
url: "../../autoComplete/autoAddQuotation1.php",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data.items
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
//templateResult: formatRepo, // omitted for brevity, see the source of this page
//templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
最后,我的 PHP
<?php
include($_SERVER['DOCUMENT_ROOT']."/1System/php/connect.php");//calling connection file
$conn=dbConnect();
if (!$conn)
die("Couldn't connect to MySQL"); /*when server is down, the statement will be showed*/
$query = "SELECT c.customerID, c.name AS cname FROM customer c WHERE c.name LIKE '%".mysql_real_escape_string(strtoupper($_GET['q']))."%' or '%".mysql_real_escape_string($_GET['q']))."%'";
$result = mysqli_query($conn, $query);
$numCustomer = mysqli_num_rows($result);
if($numCustomer != 0) {
while(row = mysqli_fetch_array($result)) {
$answer[] = array("id"=>$row['customerID'], "text"=>$row['cname']);
}
}else {
$answer[] = array("id"=>"0", "text"=>"No Results Found...");
}
echo json_encode($answer);
?>
我正在使用 mysqli 连接到我的数据库。我的连接适用于其他 php 页面。
$conn = mysqli_connect($host, $username, $password);
说实话,我真的不知道 PHP 文件应该是什么样子的?任何人都可以为我指出一个好的例子或正确的方向。
问题:
当我点击我的 chrome 上的输入时,它显示没有找到结果。![在此处输入图像描述][1] [1]:http://i.stack.imgur.com/PDAa5.png
提前致谢。如果我的问题重复或不好,请反馈。
【问题讨论】:
-
1.检查萤火虫(或其他浏览器的类似扩展)所说的内容。 2.在你的JS代码中
url参数应该是一个URL,而不是一个相对路径。 3. 你的 PHP 代码应该返回array('items' => $answer) -
@dragoste 如何检查我的 php 代码的返回结果?我读到人们能够看到他们的 json 结果,但我不太清楚我该怎么做。谢谢
-
在 Firebug -> 控制台和网络选项卡中,您可以查看所有 ajax 请求。
标签: php json ajax jquery-select2