I have a form to make the registration of a project with N participants.
I was asked now to add a field where each participant must send their school certificate.
The problem is that, adding this field inside an object, I am no longer able to send the data to PHP.
The JSON I have to send has the following format:
{
"description": [STRING],
"name": [STRING],
"objective": [STRING],
"participants": [NUMBER],
"regulament": [BOOLEAN],
"resume": [STRING],
"users": {
"1": {
"born": [DATE],
"educationCertificate": [OBJECT], // File
"email": [STRING],
"genre": [BOOLEAN],
"level": [NUMBER],
"name": [STRING],
"neighborhood": [STRING],
"phone": [STRING],
"school": [STRING],
"socialNumber": [STRING]
},
. . .
"n": {
"born": [DATE],
"educationCertificate": [OBJECT (FILE)],
"email": [STRING],
"genre": [BOOLEAN],
"level": [NUMBER],
"name": [STRING],
"neighborhood": [STRING],
"phone": [STRING],
"school": [STRING],
"socialNumber": [STRING]
}
}
}
To send the files, my $http looked like this:
$http({
method: "POST",
url: [SCRIPT],
headers: {
"Content-Type": undefined
},
data: $scope.data,
transformRequest: function (data) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
return formData;
}
}).success(function(response) {
// do something
});
And in PHP, this is my function to retrieve the data sent by AngularJS:
function param() {
$array = array();
$request = new stdClass();
if (count($_GET) || count($_POST) || count($_FILES)) {
$request = json_decode(json_encode(array_merge($_GET, $_POST, $_FILES)), false);
} else {
$request = json_decode(file_get_contents("php://input"));
}
if ($request) {
$array = array_filter(array_map(function($data) {
return is_string($data) ? trim($data) : $data;
}, get_object_vars($request)), function($data) {
return is_string($data) ? strlen($data) : $data;
});
}
return json_decode(json_encode($array), false);
}
/* In the script */
$request = param();
However, the data that is coming in PHP is as follows:
{
"description": [STRING],
"name": [STRING],
"objective": [STRING],
"participants": [NUMBER],
"regulament": [BOOLEAN],
"resume": [STRING],
"users": [STRING] // "[object Object]"
}
I always made use "Content-Type": undefined
, but I've tried to use "Content-Type": "multipart/form-data"
and "Content-Type": "application/x-www-form-urlencoded;"
. But the result is even worse.
Does anyone know how to solve this problem?