I have the following input which is a toggle returns true , false
<input id="{{event.id}}" ng-model="event.is_active" type="checkbox" value="true" class="block__input" ng-class="{'input__toggle--active' : event.is_active}">
and when I send it like this
var formData = new FormData();
console.log(scope.event.is_active);
formData.append('is_active', scope.event.is_active);
In the server I receive false and true as strings 'true', 'false'
How to solve this problem ?
FormData
will always be sent as strings. One way to solve the problem is to use JSON. Simply encode your values with JSON.stringify
on the clientside. On serverside you simply decode the values.
Clientside
var fd = new FormData;
var data = {
name: 'john doe',
active: true,
count: 42
};
var prop;
for(prop in data){
fd.append(prop, JSON.stringify(data[prop]));
}
// if you want to upload files, too
fd.append('file', file);
$http({
method: 'post',
url: '/api/upload',
data: fd,
transformRequest: angular.identity,
headers:{ 'Content-Type': undefined }
});
Serverside (PHP, simplified)
$data = [];
foreach($_POST as $prop => $value){
$data[$prop] = json_decode($value);
}
// $data contains the correct values now ..