亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法獲取數組結果來填充 jquery select2

無法獲取數組結果來填充 jquery select2

PHP
小唯快跑啊 2023-04-21 10:47:57
我正在嘗試讓 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>
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

根據規范,正確的格式是:

{

? "results": [

? ? {

? ? ? "id": 1,

? ? ? "text": "Option 1"

? ? },

? ? {

? ? ? "id": 2,

? ? ? "text": "Option 2"

? ? }

? ],

? "pagination": {

? ? "more": true

? }

}

因此,您需要遍歷結果并創建一個與該格式匹配的數組。


查看完整回答
反對 回復 2023-04-21
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

$response = mysqli_fetch_all($result);


我按如下方式循環,其他一切保持不變:


while($row = mysqli_fetch_array($result)) {

$response[] = array("id"=>$row['id'],"text"=>$row['text'])

;}

它完美地工作。


查看完整回答
反對 回復 2023-04-21
  • 2 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號