Take the 2-minute tour ×

I've been stuck with the retrieving of my sent variable trough an ajax POST function. Could you help me out?

My Jquery code:

$.ajaxSetup ({
        cache: false

var selected = new Array();    
$(document).ready(function() {       
    $('.value').click(function () {                      
            selected.splice(selected.indexOf(this.innerHTML), 1);  

function submitData(arDat)    {
    var arrayData =  {"param1" : JSON.stringify(arDat)};

        type: 'POST',
        url: 'http://localhost.local/coconut/trunk/challenges/values',           
        data: arrayData,
        dataType: 'json',
        success: function(data){
        error: function(message){

My CakePHP Controller function:

function values() {
        if ($this->RequestHandler->isAjax()) {
            $this->autoRender = false;
            Configure::write('debug', 0);
            $params = json_decode($_POST['param1']);
            //$result = json_encode($params);
            $this->set('submitValue', $params);
        } else {
            $this->redirect(array('controller' => 'challenges', 'action' => 'index'));

And in a view.ctp file:

<?php debug($submitValue); ?>

But I get the following error: Notice (8): Undefined variable: submitValue

In firebug I see this:

param1  ["Business","Life","Health"]

Does anyone know what I'm doing wrong?



A bit more clarification about what I want.. I want to use $this->set('submitValue', $params); (so $submitValue) elsewhere in another view.

share|improve this question
my initial observation is that "json_decode()" won't decode the URI encoded string as you expect, but I could be wrong there. A JSON encoded string would look like {"param1":["Business","Life","Health"]} in this case. What you're sending to the server is a URI Encoded string. –  jcolebrand Nov 28 '11 at 23:05
What is the language you are sending to the server ? and receiving –  Sedz Nov 28 '11 at 23:06
@ jcolebrand, I changed "json_decode()" to decodeURI no changes :(. –  Arvind Nov 28 '11 at 23:23
@UserB JSON strings, I suppose? –  Arvind Nov 28 '11 at 23:23
That wasn't really an answer. If it's not too much trouble, could you go ahead and post your solution? –  Michael Myers Nov 29 '11 at 19:28
show 6 more comments

1 Answer

The CakePHP function 'isAjax()' checks to see if a request is a Prototype Ajax request.

You aren't using Prototype, you're using jQuery - so presumably it's always returning false, and so submitValue is never set.

share|improve this answer
add comment

Your Answer


