1

Was wondering what I'd have to do to convert my data that is currently being sent like this (using serializeArray())

data[0][name]:title
data[0][value]:This is my sample title
data[1][name]:description
data[1][value]:This is an (optional) description but I'm filling it out as an example
data[2][name]:source
data[2][value]:http://www.cnn.com
data[3][name]:category
data[3][value]:animals

To send like this instead:

data[title]:This is my sample title
data[description:This is an (optional) description but I'm filling it out as an example
data[source]:http://www.cnn.com
data[category]:animals

Code:

$(function() {
    $('input[type="submit"]').click(function() {
        var $data = $('#js-serialize').serializeArray();
        console.log($data);
        $.post('/echo/json', {
            'data': $data
        }, function(data) {
            console.log(data);
        }, 'json');
        return false;

    });

});

<form id="js-serialize">
<input name="title" placeholder="title" />
<input name="description" placeholder="description" />
<input name="source" placeholder="source" />
<input name="category" placeholder="category"/>
<input type="submit" name="submit" />
</form>​

Demo:

http://jsfiddle.net/someprimetime/bnPwN/5/

bob_cobb
  • 2,229
  • 11
  • 49
  • 109
  • What is the purpose of structuring it differently? Unless you specify otherwise, jQuery will turn it into the same thing when it sends it to the server anyway. Form params. – Kevin B Aug 21 '12 at 19:49
  • @KevinB because of the way CakePHP works, I've found that this is the only acceptable way to "serialize" my form without having to specifically send each value individually. – bob_cobb Aug 21 '12 at 19:50
  • Right, but won't jquery take whatever data you pass in whether it's an object, array of objects, or a simple query string, and convert it to the same form parameters to be sent? – Kevin B Aug 21 '12 at 19:51
  • With your current code, you should see `$_POST["description"]` and it should also be available in the cakePHP format, unless it's doing something different behind the scenes. – Kevin B Aug 21 '12 at 19:54
  • @KevinB Kind of... when I set `$this->request->data` to my model, it has to in a particular format such as `$this->request->data['title']` or something similar (without the name/value index). I mean I could do it like that, but it's just easier for me to do it like this in the controller. – bob_cobb Aug 21 '12 at 19:58
  • What I was getting at is that regardless of whether you pass in a querystring, an array of objects, or an object, you end up with the same thing. http://jsfiddle.net/bnPwN/6/ (check console) I'm not sure why that would make any difference in CakePHP, but if it works it works i guess. – Kevin B Aug 21 '12 at 20:13

2 Answers2

0

It seems that you're looking for an object representation instead of an array.

jQuery does not support something like serializeObject() out of the box. This plugin, however, seems to do exactly what you want.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
0

Not tested, but:

function serializeObject(serializedArray){
    var serializedObj={};
    serializedArray.forEach(function(i){
       serializedObj[i.name]=i.value;       
    });
    return serializedObj;
}
de3
  • 1,890
  • 5
  • 24
  • 39