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?

2
  • what is projcats? you dont send such a parameter Commented Aug 24, 2014 at 13:21
  • sorry. forgot to edit that part. It's categories no projcats. Commented Aug 24, 2014 at 13:23

2 Answers 2

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;
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

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..?
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.
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..
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.
Thank you immensely for your help and explanation. I think I get it now.
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'].

2 Comments

sorry. forgot to edit that part. It's categories no projcats.
but i think i see where i went wrong based on your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.