多选列表中的选定值未显示在php

时间:2019-03-09 19:44:12

标签: javascript php jquery mysql

我创建了一个多选下拉列表。我想将选定的值发送到我的PHP代码,然后用它来创建mysql查询。不幸的是,我无法在php中访问这些变量。

这是我的代码。

function Participants(sid) {
    console.log(sid); //the sid identitifies the market
    $('#participants').empty();
    $('#participants').append("<option>Loading......</option>");
    $.ajax({
        method: "POST",
        url: "participants_dropdown.php?sid=" + sid,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            $('#participants').multiselect('destroy');
            $('#participants').empty();
            $.each(data, function(i, item) {
                $('#participants').append('<option value="' + data[i].facility_id + '" >Call-Sign: ' + data[i].call_sign + ' - Operator: ' + data[i].operator + ' - Primary Programming: ' + data[i].primary_programming + '</option>');
            });
            $('#participants').multiselect({
                buttonWidth: '400px',
                includeSelectAllOption: true,
                nonSelectedText: 'Select an Option',
                onChange: function(option, checked) {
                    var selected = this.$select.val();
                    if (selected.length > 0) {
                        console.log(selected);

                        $.ajax({

                            url: "selected_participants.php?sid=" + sid,
                            method: "POST",
                            data: {
                                selected: selected
                            },

                            success: function(data) {
                                console.log('success with participating facility ids');

                                $('#atsc1_host1').append("<option value='0'>---Select Station--</option>");
                                $.each(data, function(i, item) {
                                    $('#atsc1_host1').append('<option value="' + data[i].facility_id + '">' + data[i].call_sign + '</option>');
                                });
                            },
                            complete: function() {}
                        });

                    }
                }
            });
        },
        complete: function() {}
    });
}

HTML:

<div class="col-md-4" style="color: black;">
<h3><span>2) Select Market Participants</span></h3>
<h6><select id="participants" multiple class="form-control" style="color:gray;" >
<option value="" disabled selected>Choose your a market first</option><h6>
</select>
<button class="btn-save btn btn-primary btn-sm">Save</button>
</div>



<?php
    $sid=$_GET['sid'];
    $selected_list=$_POST['selected'];
    var_dump($selected_list);
    $selected_list2 = join(",",$selected_list);
    var_dump($selected_list2);
  ?>

多选列表已正确填充,但是当我尝试将所选值发布回PHP,然后运行另一个mysql查询时,我在控制台中收到以下消息:

[“ 306”] **这意味着它可以正确看到我选择的电台

market_plans.php:152参与机构ID成功 ***,这意味着第二个ajax请求已成功执行

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in array(2) {
  [0]=>
  string(3) "306"
  [1]=>
  string(5) "59988"
}
string(9) "306,59988"

***似乎表明我正在为我的$ selected变量获取数组,但以某种方式无法访问。

我以为是因为我要传递一个数组,所以我尝试了PHP中的join和implode函数,但这并不能解决我的问题。

任何想法或帮助将不胜感激!

我将在php页面上添加以下输出:NULL string(0)。因此,它似乎不是在读取json数据问题。

这是mysql语句:

$query = "SELECT 
        station_table.call_sign, 
        station_table.facility_id, 
        station_table.operator, 
        station_programming.primary_programming, 
        station_programming.primary_resolution, 
        rt1.resolution_type AS primary_resolution_type, 
        station_programming.d_2_programming, 
        station_programming.d_2_resolution, 
        rt2.resolution_type AS d_2_resolution_type, 
        station_programming.d_3_programming, 
        station_programming.d_3_resolution,
        rt3.resolution_type AS d_3_resolution_type, 
        station_programming.d_4_programming, 
        station_programming.d_4_resolution, 
        rt4.resolution_type AS d_4_resolution_type, 
        station_programming.d_5_programming, 
        station_programming.d_5_resolution, 
        rt5.resolution_type AS d_5_resolution_type, 
        station_programming.d_6_programming, 
        station_programming.d_6_resolution, 
        rt6.resolution_type AS d_6_resolution_type 
    FROM market_table 
    INNER JOIN station_table ON market_table.nielsen_dma_code=station_table.nielsen_dma_code 
    INNER JOIN station_programming on station_table.facility_id=station_programming.facility_id 
    LEFT JOIN resolution_table AS rt1 on station_programming.primary_resolution=rt1.resolution 
    LEFT JOIN resolution_table as rt2 on station_programming.d_2_resolution=rt2.resolution 
    LEFT JOIN resolution_table as rt3 on station_programming.d_3_resolution=rt3.resolution 
    LEFT JOIN resolution_table as rt4 on station_programming.d_4_resolution=rt4.resolution 
    LEFT JOIN resolution_table as rt5 on station_programming.d_5_resolution=rt5.resolution 
    LEFT JOIN resolution_table as rt6 on station_programming.d_6_resolution=rt6.resolution 
    WHERE 
        station_table.nielsen_dma_code ='".$sid."' AND
        station_table.facility_id in (".$selected_list2.") 
    ORDER BY station_table.call_sign asc";

0 个答案:

没有答案