Balsara Dhaval Balsara Dhaval - 4 months ago 13
PHP Question

How to pass the select box value as an array using AJAX from one page to another in PHP?

var message = $("#send_message").val();

var Teaminput = $("#sms_reminder_team").val();
for (var i = 0; i <Teaminput.length; i++)
{
var team=Teaminput[i];
}

var Memberinput = $("#sms_reminder_members").val();
for (var i = 0; i <Memberinput.length; i++)
{
var members=Memberinput[i];

}


Get 2 varaibles as array members and team

var parameter = "message="+message+"&team="+team+"&members="+members;
$.ajax({
url: base_url+'ajaxfiles/dir_sendmessage',
type: 'POST',
data: parameter,
success: function(data)
{
document.getElementById('check').innerHTML = data;
}
});


How to send both array variables using AJAX from current page to "dir_sendmessage".

Answer

Change the below line

var parameter = "message="+message+"&team="+team+"&members="+members;

to

var parameter = "message="+message+"&team="+JSON.stringify(team)+"&members="+JSON.stringify(members); 

Edit: Modify like this too

var team = [];
var members = [];
for (var i = 0; i <Teaminput.length; i++) 
{
   team=Teaminput[i];
}

var Memberinput = $("#sms_reminder_members").val();
for (var i = 0; i <Memberinput.length; i++) 
{
   members=Memberinput[i];
}

Note: When you add var in each line in the loop, it will declare a new variable. You have to edit like the above code

After update the code with the JSON.stringify() function, you will be able to get the value as an array in you PHP code

Ajax will not directly pass Jquery array to PHP

Comments