我正在尝试让 jQuery SELECT2 从 mysqli 查询中提取数据:
[fxn/fxn_select2_series.php]
if(!isset($_POST['searchTerm'])){
$qry_select2_series =
"SELECT DISTINCT series_id as 'id', series_title as 'text'
FROM `series` WHERE series_title IS NOT NULL" ;
}else{
$search = $_POST['searchTerm'];
$qry_select2_series =
"SELECT DISTINCT series_id as 'id', series_title as 'text'
FROM `series` WHERE series_title IS NOT NULL and series_title LIKE '%".$search."%'" ;
}
$cxn = new mysqli('localhost', $user, $pass, $db);
$result = mysqli_query($cxn,$qry_select2_series);
$response = mysqli_fetch_all($result);
echo json_encode($response);
如果我自己打开那个页面,我会得到正确的数组格式结果:
[["1","Spring Revels"],["2","Trois Chansons"]]
我将查询插入 jQuery SELECT2 ...
<fieldset>
<legend>Title</legend>
<div class='inputlabel'>
<label for='title'>Title</label>
<input type='text' size='64' id='title'>
<label for 'subtitle'>Subtitle</label>
<input type='text' size='64' id='subtitle'>
<label for='series'>Series</label>
<span><select class="js-example-basic-single" id='select2_series' style='width:32'>
<option>Select/Search ...</option>
</select>
(Enter Opus, Collection, Volume, Libretto, Etc. or select below...)</span>
<label for='sequence'>Sequence</label>
<span><input type='number' min='1' max='99' step='1' size='4' id='sequence'>
(Enter the sequence number of this item in the above series)</span>
</fieldset>
<script type="text/javascript">
$(document).ready(function(){
$("#select2_series").select2({
ajax: {
url: "fxn/fxn_select2_series.php",
type: "post",
dataType: 'json',
delay: 250,
data: function (params) {
return {
searchTerm: params.term // search term
};
},
processResults: function (response) {
return {
results: response
};
},
cache: true
}
});
});
</script>
肥皂起泡泡
qq_遁去的一_1