1

I want to take all elements from each 2 rows if a checkbox is chekced and make them in a json array and send it with ajax to the php.

$('input.pay').each(function(){
    if($(this).is(':checked'))
    {
        row = $(this).parents('tr').attr('id').split('_');
        type = row[0];
        payID= row[1];
        payJsonArr = '';
        secondRow=', #'+type+'_'+payID+$('#rate_'+payID).attr('class');

        $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
            if($(this).is('input') || $(this).is('select'))
                payJsonArr += $(this).attr('name') + ':' + $(this).val()+',';   
            else if($(this).is('td') || $(this).is('span'))
                payJsonArr += $(this).attr('name') +':'+ $(this).html().trim()+',';
        });
        payJsonArr += payJsonArr.substring(0, payJsonArr.length - 1);
        payments[payID]= '{'+payJsonArr+'}';
    }
});

The problem is that with that code i get the array in php i get the fallowing:

array(1) {
  [791]=>
  string(501) "{field 1:2012-10-07,field 2:6777.00 }"
}

How can i get it like it should if it was a JSON,like that :

array(1) {
  [791]=>
    [field 1]=>'2012-10-07',
    [field 2]=>'6777.00'
}

If anybody can help i would appreciate it.Thank you all for helping those in need.

3
  • 1
    there's no such thing as a "json array". There's json strings, which contain textual representations of a javascript data structure. You don't "build a json array". You build a JS data structure, then convert it to json as the very last step before sending that json string somewhere else. It would appear that you're double-encoding: building your own (invalid/broken) json string, which jquery than re-json-encodes. Commented Oct 11, 2012 at 16:04
  • 3
    the json is invalid due to lack of quotes Commented Oct 11, 2012 at 16:04
  • Actually everything can be interpreted as an array.Aspecially a string.Like $string = 'abc' and you can get 'c' like that $string[2].Thank you for your pointless clarification.I am fully aware of what JSON is.Just the last part would have done the job :"the json is invalid due to lack of quotes". Commented Oct 12, 2012 at 7:07

3 Answers 3

2

You can also use like that,

    `var payJsonArr = [];
    $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
        if($(this).is('input') || $(this).is('select'))
            payJsonArr[$(this).attr('name')] = $(this).val();   
        else if($(this).is('td') || $(this).is('span'))
            payJsonArr[$(this).attr('name')] = $(this).html().trim(); 
    });

    payments[payID]= payJsonArr;`

If you want to learn more about JSON then you can see some nice examples here : JSON

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

1 Comment

This worked exactly how i wanted with 2 small fixes.It doesn't work if var payments = []; and var payJsonArr = [];It works though if you define them like obejcts: var payemnets = {};var payJsonArr={}; Thank you for the great tip.I appreciate it.
1

Building on what Rohit suggested, try this (this is untested):

$('input.pay').each(function(){
if($(this).is(':checked'))
{
    row = $(this).parents('tr').attr('id').split('_');
    type = row[0];
    payID= row[1];
    payJsonArr = {};
    secondRow=', #'+type+'_'+payID+$('#rate_'+payID).attr('class');

    $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
        if($(this).is('input') || $(this).is('select'))
            payJsonArr[$(this).attr('name')] = $(this).val();   
        else if($(this).is('td') || $(this).is('span'))
            payJsonArr[$(this).attr('name')] = $(this).html().trim();
    });
    payments[payID]= payJsonArr;
}

});

2 Comments

Lol,should have looked at your post earlier :) defining the arrays like object {} is exactly what was missing from the post above.Thanks you all.You helped me alot :)
@LachezarRaychev No prob. If this is what you needed, please mark this as the correct answer :)
0

You should try to create a string which can be converted into JSON. For this create the string eg:

var jsonString= {
             "field1":"value",
             "field2":"value" 

           };

Always keep in mind if you are using double quotes and then use only double quotes and if using single quote then always use single quote.

Comments

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.