My current project is passing multiple values to php via ajax and I want to add an array together but I failed to do so.
current JS file:
var name= 'John';
var age= 21;
var gender = 'm';
var postData = 'name='+name+'&age='+age+'&gender=gender';
$.ajax({
type:'POST',
dataType:'json',
url:baseUrl+'/student',
data:postData,
}).done(function (data){
alert('success');
}
});
What I want to add:
var subject = ["math","geograph"];
JSON.stringify(subject ); //to encode the array
I have tried:
var postData = 'name='+name+'&age='+age+'&gender='+gender+'&subject='subject';
and
data:{subject : subject , postData : postData},
and I get this array in php:
$subject = json_decode($request['subject']),true);
But I can't get the array in my php. How can I solve this problem? Thanks