0
$.fn.fieldValues = function(successful)
{
    var values = {};
    this.each(function()
    {
        if(strstr(this.name, '[]', true))
        {
            var tmp = this.name.replace(/\[\]/, '');
            if(typeof values[tmp] == 'undefined') values[tmp] = {};
            var x = 0;
            while(typeof values[tmp][x] != 'undefined') x++;
            values[tmp][x] = $(this).val();
        }
        else values[this.name] = $(this).val();
    });
    return values;
}

problem is I get this array on php side:

array(['tagCloud'] => '[object Object]', ['status'] => 'Active'.....)

Why is tagCloud an object, how can I post a whole associative array to php?

3 Answers 3

1

Sounds like you need SerializeArray instead, which works like Serialize but will make an array of name/value objects.

You should then turn this into a JSON string and pass through to your php process. The php can then deserialize it back into an array of name/value objects and you can use the data however you want.

//build json object
var dataArray = $.makeArray($("form").serializeArray());

then pass through as a post:

// make ajax call to perform rounding
$.ajax({
    url: "/Rounding.aspx/Round/12",
    type: 'POST',
    dataType: 'html',
    data: $.toJSON(dataArray),  <-- call to jQuery plug in
    contentType: 'application/json; charset=utf-8',
    success: doSubmitSuccess
});

Here is a link to the JSON library I use to serialize the data

Sign up to request clarification or add additional context in comments.

Comments

1

Would encoding it as a json object and then decoding it in php (json_decode) work out?

3 Comments

Yes, I think it would, but JQuery native hasn't got json native, and every plugin I used sucks, anyone know of a good plugin for this to work?
You mean that PHP doesn't have JSON native?
No, but I found a solution, problem was that when using toJson in JQuery I got object of stdClass, I need to convert this to array on php side and it will be OK.
0

It looks like you're re-inventing jQuery.fn.serialize. jQuery handles inputs with "[]" in the name already:

<form>
    <input type="hidden" name="foo[]" value="1" />
    <input type="hidden" name="foo[]" value="2" />
    <input type="hidden" name="foo[]" value="3" />
</form>

<script>
alert(unescape($('form').serialize())) // "foo[]=1&foo[]=2&foo[]=3"
</script>

php will parse that into an array OOTB.

3 Comments

Yes but serialize returns string, and I need array cause I will handle it diferent with my custom ajax function
@dfilkovi: define "handle it diferent" please.
Well first I fetch all data from a form, then I put other needed variables into that array, like wich php function to call (update, delete) and then pass that whole array to ajax. By using serialize, I would need to add all other variables to that string, but what when I'm not using serialize() but simple val() and want to pass only one variable. I would need to check If any of the passed variables are serialized strings and then update string with other variables, seems to me like an overkill.

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.