views:

83

answers:

3

Hi, i'm trying to pass a javascript array to a php controller ( i'm using codeigniter ) with ajax post method. Data seems to be sent but $_POST['data'] is not known. This is the code :

JAVASCRIPT:

function update_order(){
var ordre_column1 = $('#column1').sortable('toArray');
var data = serialize(ordre_column1);

$.post('../../controlleur_groupe_admin/ordre_box',data);
}

MY CONTROLLER:

function ordre_box() {
    $data = $this->input->post('data')
    $array = unserialize($data);
    print_r($array);
}

I got no return in firebug, i'm wondering if content type is wrong:

Content-Type application/x-www-form-urlencoded; charset=UTF-8

thanks.

To simplify the code a bit :

Javascript :

function update_order(){
var ordre_column1 = $('#column1').sortable('toArray');
var data = ordre_column1.toString();

$.post('../../controlleur_groupe_admin/ordre_box',data);
}

Controller :

function ordre_box() {
    echo $_POST['data'];
}

Firebug say :

Message: Undefined index: $data

But the post exists : Paramètresapplication/x-www-form-urlencoded 131,126,125,156,154 Source 131,126,125,156,154

A: 

What's your serialize function do? If it isn't moving that array to a string format you likely aren't going to see anything in your controller.

ryan
i don't really knows what serialize do, i only read the tip that unserialize will convert my array in a php array. here is the doc: http://www.php.net/manual/en/function.serialize.php. But before using it, my code was that and i have the same problem :function update_order(){var ordre_column1 = $('#column1').sortable('toArray');var data = ordre_column1.tostring()$.post('../../controlleur_groupe_admin/ordre_box',data);}then i used explode to make an array.
Joeyjoejoe
+1  A: 

How would you know that the key is 'data', wouldn't it be whatever you passed in the serialized string?

For example, if you had an array like this:

$array['value'] = 'hey!';

And you serialized this and sent it to the controller, you would retrieve that value like this:

$this->input->post('value');

Not like this, which I what I think you're trying to do but I might be wrong:

$array = $this->input->post('data');
echo $array['value'];

To fix it you could put the js array into another array with a key called data and then serialize that..

Calle
In fact `$('#column1').sortable('toArray')` return a list of all box id contained in #column1, i have that array : array[0] = first idarray[1] = second id ... etc And i need to retrieve all that values in a php array. I m not sure understanding well what you said ( my english ).
Joeyjoejoe
serialize, in jquery context, is meant to be used on form elements. And in that scenario, the names of the form elements would be the keys. If you don't have any keys, you can't retrieve the data later. I see now that you changed serialize and put "toString" instead. This is wrong, all that does it convert a variable of boolean type into a variable of string type. This is not what you want. I think you need something like this but I don't know if you will understand it: http://bit.ly/UAbHy I wish I could explain this better, maybe someone else can.
Calle
I already put an eye on that topic, and it seems to be the best solution to my problem. But i'm a bit lazy, so i tried more simple things... i shouldn't in fact ;). thanks, i ll post the solution when i ll found it.
Joeyjoejoe
+1  A: 

Ok, i found the solution! Here is the code ( with jquery.json-2.2.min.js ):

Javascript :

function update_order(){
var items=[];    // This common array will get all info for each item.
var ordre_column1 = $('#column1').sortable('toArray');
for (var i in ordre_column1){    //create an array for a single item
var item ={id: ordre_column1[i],
           column_id: 1,
           sort_no: i
};
items.push(item);   // put the single item array in common array
}
var ordre1={ items: items };

$.post('../../controlleur_groupe_admin/ordre_box','data='+$.toJSON(ordre1));  //post the data to JSON format
}

The data are sent in JSON to the controller.

Controller :

function ordre_box() {
    $data = $_POST['data'];
    $json = str_replace('\\','',$data); //we replace all backslashes with nothing, which results in a correct json_decode string
    $newdata = json_decode($json);  // decode JSON format to php array
    foreach($newdata->items as $item){  // Now i can use the data
        echo " objet :";
        echo $item->id;
        echo ",";
        echo $item->column_id;
        echo ",";
        echo $item->sort_no;
    }
}

Thanks to Calle for putting me on the good way.

Joeyjoejoe