0

I have created a multi-select dropdown. I want to send the selected values to my php code which I then use to create a mysql query. Unfortunately, I am not able to access those variables in php.

Here is my code.

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);
  ?>

The multi-select list is correctly populated but when I attempt to post my selected values back into PHP to then run another mysql query, I get the following message in my console:

["306"] **Which means that it correctly sees my selected station

market_plans.php:152 success with participating facility ids ***which means that the second ajax request was successfully executed

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"

***which appears to indicated that I am getting an array for my $selected variable but which is somehow unaccessible.

I thought it was because I was passing an array, so I tried both the join and implode functions in PHP, but that did not resolve my problem.

Any thoughts or help would be appreciated!

I would add that the output on my php page states: NULL string(0). So it does not appear to be reading of json data problem.

Here is the mysql statement:

$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";
Dharman
  • 30,962
  • 25
  • 85
  • 135
user3003374
  • 73
  • 1
  • 10
  • Possible duplicate of [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – Dharman Mar 09 '19 at 20:04
  • I have clarified my question to distinguish it from these prior questions – user3003374 Mar 09 '19 at 20:22
  • Are you trying to loop in JavaScript on the output of `var_dump()`? That is not going to work. Use `json_encode()` and then parse the JSON in your success callback. – Dharman Mar 09 '19 at 20:28
  • I am trying to populate a "in" statement in mysql with the ids of the selected stations. I have revised my join statement and am now getting this error message. Unknown column 'Array' in 'where clause'. When I echo mysql query the in statement shows up as () – user3003374 Mar 09 '19 at 20:33
  • I can't see any SQL statement in your question. The previous error was from JavaScript. – Dharman Mar 09 '19 at 20:34
  • I have revised my question to provide more details. The mysql statement is very long but I know it works when I run it in mysql workbench(without the dyanamically populated variables in the "in" statement ....the problem is that the variables that populates the "in" statement generates a type error. – user3003374 Mar 09 '19 at 20:37
  • If you are getting an error from the SQL query, then show us that query. – Dharman Mar 09 '19 at 20:39
  • Added the relevant mysql query to the question – user3003374 Mar 09 '19 at 20:46
  • **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](http://php.net/manual/en/pdo.prepared-statements.php) or by [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). – Dharman Mar 09 '19 at 20:50
  • Have you ever heard of [DB normalization](https://en.wikipedia.org/wiki/Database_normalization)? – Dharman Mar 09 '19 at 20:52
  • I have not heard of it, but I understand the concept after reviewing. Seems like a good idea to improve the database structure. I just imported existing tables making sure that no field was repeated in any table. Thanks for the input. Any thoughts about why my variable is not being seen in php? It is now saying: NULL Warning: join(): Invalid arguments passed – user3003374 Mar 09 '19 at 21:00
  • try echoing out your query before it runs and see if you see the issue – dmikester1 Mar 09 '19 at 21:07
  • Thanks. I echo the query and here is the current error message: Warning: Use of undefined constant console - assumed 'console' (this will throw an Error in a future version of PHP) in /homepages/35/d448461721/htdocs/selected_participants.php on line 31 Warning: log() expects parameter 1 to be float, string given in /homepages/35/d448461721/htdocs/selected_participants.php on line 31 . This is because when i echo the variable $selected_list2 I get a NULL value – user3003374 Mar 09 '19 at 21:19
  • Hi all. Thanks everyone for your responses. I figured it out. It was not working as a POST function for some reason, but I got it to work via a GET function (from URL) – user3003374 Mar 09 '19 at 22:55

0 Answers0