I have the following scenario, I have an Angular variable called contact which is a JSON object that has the following members:
- Message
- Name
- Phone
I console.log(contact) and it logs the correct information, however once sent, $_POST['contact'] results in an empty array. I was reading around and found that $_POST['contact'] won't get populated like it would if it were an http request from jQuery. So I fixed it to the following:
$params = json_decode(file_get_contents('php://input'),true);
print_r($params);
And here is the Angular function:
$scope.submitForm = function(contact){
console.log(contact);
$scope.clearContactFormData();
return $http.post('http://localhost/cpr_courses/app/methods/contact.php', contact).success(function(data){
Materialize.toast(data, 4000);
});
}
Still the print_r($params) is empty. I have no idea what else is going on. Any help please?
EDIT
console.log() outputs the following:
Array[0]
email
:
"[email protected]"
length
:
0
message
:
"rubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOBrubAWREIUVBAERWOBNATEIOB"
name
:
"rubAWREIUVBAERWOBNATEIOB"
phone
:
"13345678"
and here is the network tab image:
Here's the declaration of the JSON object:
$scope.contact = {};
$scope.onlyNumbers = /^[0-9]+$/;
This is later put on on the form like this:
<form method="post" class="form-sl" role="form" name="contactForm" ng-submit="submitForm(contact)" novalidate enctype="multipart/form-data">
and later on, on each field like this:
<input type="email" name="email" id="email" ng-model="contact.email" autocomplete="off" ng-maxlength="50" length="50" required/>
Is that okay?

file_get_contents('php://input')in php (without json_decode) and see what php's getting.{}and not with[]... that log seems quite weirdcontactvariable is a keyed array. Given the length of the array is 0, Angular is probably not bothering to check if there's any data in it. Make yourcontactvariable an Object instead