Lachezar Raychev
Lachezar Raychev

Reputation: 2113

Make a json array with elements name and value and give it to php with ajax

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.

Upvotes: 1

Views: 649

Answers (3)

Dipak Patil
Dipak Patil

Reputation: 93

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

Upvotes: 2

Seth
Seth

Reputation: 1373

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;
}

});

Upvotes: 1

Rohit Choudhary
Rohit Choudhary

Reputation: 2259

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.

Upvotes: 0

Related Questions