user253530
user253530

Reputation: 2591

jquery array of checkbox not sent to php file

to gather the value from the check boxes:

var filtersArray = $("input[@name='filters']:checked").map(function(i,n){
                        return $(n).val();
                    }).get();

Posting to php file

$.post("php/performSearch.php", {
     keywords: $('#keywords').val(), 
     'filters[]': filtersArray}, 
     function(data){
         //alert(data);
     });

Php doesn't get the array no matter what i do to it I have:

$postedKeywords = $_POST['keywords'];
$postedFilters = $_POST['filters[]'];

Keywords is posted, filters[] is not. I tried print_r....no result..

I tried:

foreach($_POST as $val)
    echo $val;

I get the value of $_POST['keywords'] and Array for $_POST['filters'] So it is sent but for some reason i cannot use the values.

Upvotes: 1

Views: 329

Answers (2)

Sarfraz
Sarfraz

Reputation: 382666

Use this:

$.post("php/performSearch.php", {
     keywords: $('#keywords').val(), 
     'filters\[\]': filtersArray}, 
     function(data){
         //alert(data);
     });

Or try this too:

$.post("php/performSearch.php", {
     keywords: $('#keywords').val(), 
     filters: filtersArray}, 
     function(data){
         //alert(data);
     });

And to get in php, don't use [], just use:

$postedKeywords = $_POST['keywords'];
$postedFilters = $_POST['filters'];

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Have you tried:

$.post(
    'php/performSearch.php', {
        keywords: $('#keywords').val(), 
        filters: filtersArray
    }, 
    function(data) {

    }
);

This will send a POST request that might look like this:

filters[]=1&filters[]=2

Upvotes: 2

Related Questions