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.

pbaris
  • 4,525
  • 5
  • 37
  • 61
MD TAHMID HOSSAIN
  • 1,581
  • 7
  • 29
  • 54
  • Convert your array to a JSON string - http://stackoverflow.com/questions/191881/serializing-to-json-in-jquery – Styphon Jul 01 '13 at 13:08
  • `$('#friend_id_saf :checked').each(function() {` seems you're using duplicate ID's, ID's should be unique! – tymeJV Jul 01 '13 at 13:10
  • I guess you can refer to this SO, http://stackoverflow.com/questions/5377585/passing-array-value-from-jquery-ajax-to-php-function-call. – dreamweiver Jul 01 '13 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. – dreamweiver Jul 01 '13 at 13:15
  • Make a Fiddle with your html. Guys, always try to save others time. – Nono Jul 01 '13 at 13:16
  • @dreamweiver getting Uncaught SyntaxError: Unexpected token ILLEGAL data:{'search_user_id':search_user_id,'type':type,'myArray':al‌​lVals}, //Uncaught SyntaxError: Unexpected token ILLEGAL what to do man? – MD TAHMID HOSSAIN Jul 02 '13 at 09:12
  • 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 – dreamweiver Jul 02 '13 at 10:04
  • With respect to above comment ,**value1** & **value2** are string constanst,hence enclosed in singles quotes. if you wanna pass variables, then use the variable name without any quotes. – dreamweiver Jul 02 '13 at 10:16
  • @dreamweiver i have fixed it now. thank you very much. – MD TAHMID HOSSAIN Jul 02 '13 at 11:07
  • ok, are you saying what i suggested worked out for u ? – dreamweiver Jul 02 '13 at 11:32
  • yup it was. thank you very much – MD TAHMID HOSSAIN Jul 02 '13 at 17:22

1 Answers1

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
Nix
  • 57,072
  • 29
  • 149
  • 198