【问题标题】:cannot get array results to populate jquery select2无法获取数组结果以填充 jquery select2
【发布时间】:2020-09-14 20:45:58
【问题描述】:

我正在尝试让 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>

但是当你在 Select2 对象的搜索框中输入内容时,页面什么也拉不出来……

【问题讨论】:

  • 这只是一个测试,不是生产环境,我会在它上线之前对所有东西进行消毒。
  • 不清理数据。你真的应该参数化你的 SQL。
  • 我通常将 SQL 语句放在它们自己的 *.sql 文件中,并在必要时将它们作为变量包含在内。更容易对数据库运行直接查询并根据需要进行调整,而无需触及 php 包装器的其余部分。

标签: javascript php jquery mysqli jquery-select2


【解决方案1】:

根据specs,正确的格式是:

{
  "results": [
    {
      "id": 1,
      "text": "Option 1"
    },
    {
      "id": 2,
      "text": "Option 2"
    }
  ],
  "pagination": {
    "more": true
  }
}

因此,您需要遍历结果并创建一个与该格式匹配的数组。

【讨论】:

    【解决方案2】:

    感谢 imvain2 提供查看输出格式的建议。确切的解决方案......而不是

    $response = mysqli_fetch_all($result);

    我循环如下,保持一切不变:

    while($row = mysqli_fetch_array($result)) {
    $response[] = array("id"=>$row['id'],"text"=>$row['text'])
    ;}
    

    而且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 2018-08-01
      相关资源
      最近更新 更多