0

Guys i want to pass an array (got from checkbox) with some other variable.

function some_function(user_id,add_remove_id,which_page) {
    //getting the array
    var allVals = [];   
    $('#friend_id_saf :checked').each(function() {
        allVals.push($(this).val());
    });
    var type=3;
    var data = 'user_id=' + user_id+'&add_remove_id='+add_remove_id+'&type='+type;

    $.ajax({
        type:"POST",
        url:"add_rem_friend.php", 
        //*****************************************
        // how should i send the data to php
        data:data,{myarray:allVals}, 
        //******************************************          
        success:function(html) {
            if (which_page==='incoming-request') {
                $('#'+add_remove_id).html(html);

            } else if(which_page ==='profile-details') {
                $('#div_status').html(html);
            }
        }
    });

    return false;
}

how should i send the array with other data?

data:data,{myarray:allVals}, 

please help me.

11
  • Convert your array to a JSON string - stackoverflow.com/questions/191881/… Commented Jul 1, 2013 at 13:08
  • $('#friend_id_saf :checked').each(function() { seems you're using duplicate ID's, ID's should be unique! Commented Jul 1, 2013 at 13:10
  • I guess you can refer to this SO, stackoverflow.com/questions/5377585/…. Commented Jul 1, 2013 at 13:10
  • 1
    instead of merging them like this var data = 'user_id=' + user_id+'&add_remove_id='+add_remove_id+'&type='+type; , better place them all in ajax call like this .data:{'user_id':user_id,'add_remove_id':add_remove_id,'type':type,'myArray:'allVals'}. this is simplest way of passing the values in ajax call. Commented Jul 1, 2013 at 13:15
  • 1
    @MDTAHMIDHOSSAIN:Sorry man, i told you the wrong way. try this way data: { start: 'value1', end: 'value 2' }. let me know if you are still getting error Commented Jul 2, 2013 at 10:04

1 Answer 1

1

I think its as simple as just merging the maps.

data: $.extend(data, {myarray:allVals});

or if its simple set it before the call and just pass data.

#before .ajax
data[myarray] = allVals

#inside of .ajax
data: data
Sign up to request clarification or add additional context in comments.

1 Comment

data: $.extend(data, {myarray:allVals}); should be in ajax call?

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.