0

I have a list of options (categories) of projects that the user can see. By selecting the categories, the div below should update with the lists of projects matching said categories.

Despite using the following answer, almost verbatim, Send array with Ajax to PHP script, I am still unable to retrieve any results, and yet no errors show up either.

The jquery:

// filter for projects
var $checkboxes = $("input:checkbox");

function getProjectFilterOptions(){
    var opts = [];

    $checkboxes.each(function(){
        if(this.checked){
            opts.push(this.name);
        }
    });

    return opts;
}

$checkboxes.on("change", function(){
    var opts = getProjectFilterOptions();

    //alert(opts);

    var categories = JSON.stringify(opts);

    $.ajax({
        url: "/web/plugins/projcat.php",
        type: "POST",
        dataType: "json",
        data: {data : categories},
        cache: false,

        success: function(data) {
            $('#projects').html(data);
            //alert(data);
        }
    });

});

the php (still in testing, so not filled out):

<?php

if(isset($_POST['categories'])) {

    //echo "testing";
    $data = json_decode(stripslashes($_POST['categories']));
    print_r($data);
}
?>

Where is the error?

Community
  • 1
  • 1
obmon
  • 347
  • 2
  • 12

2 Answers2

1

Try this:

JS

...    
// dataType: "json",   // remove that; you're not sending  back JSON string
data: {categories : categories},
cache: false,
...

PHP

<?php

if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['categories'])) {

    //echo "testing";
    $data = json_decode(stripslashes($_POST['categories']));

    // .. process the $data

    print_r($data);
    return;
}
?>
  • that works! Thanks, but you could explain where i went wrong? I would like to understand, why i can't use {data: categories}, and why the extended if..? – obmon Aug 24 '14 at 13:31
  • The `data:{data:..}` is not needed. Otherwise you needed to process your data above as `$_POST["data"]["categories"]`. Secondly, the `dataType` is used on what data you're sending back! `print_r()` is certainly not an `encoded json string` :) That was the second error. –  Aug 24 '14 at 13:34
  • if data:{data:..} is not needed, that means the line could be data: categories, ...? I tried it, and it didnt work. It has to be data: {categories:categories}.. I'm finding that line confusing.. – obmon Aug 24 '14 at 13:40
  • 1
    I know :) Just think that `data:{..}` is stripped off when i comes to server. A `data:{data2:"text"}` needs to be accessed as `$_POST["data2"]` but this `data:{text:"mytext"}` needs to be accessed as `$_POST["text"]`. Just adding two `datas` increases the complexity. Dont get confused by the `categories:categories`, the first is the key and the other your array. It's perfectly ok to have the same name since JS understand its an object. –  Aug 24 '14 at 13:46
  • Thank you immensely for your help and explanation. I think I get it now. – obmon Aug 24 '14 at 13:54
0

Your desired array is in $_POST['data'] and not in $_POST['projcats']. Change data: {data : categories}, to data: { projcats: categories }, to use $_POST['projcats'].

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39