2

Not able to make $http post request, getting undefined for $_POST["name"] in php and for all other posted data. but in my console printing all the data correctly, can u help me where I did mistake. I am sending data when click event is triggered, I am new to angular, please help me to solve this problem, Thanks to replies in advance.

angular.element(document.querySelector('#applyJob')).unbind('click').bind('click', function () {
    console.log($scope.userName+$scope.userEmail+$scope.userMobileNo+$scope.subject+$scope.userCoverLetter+$scope.attach);
    $http({
        method: "POST",
        url: "mailer.php",
        data: {
            name : $scope.userName,
            mail : $scope.userEmail,
            no : $scope.userMobileNo,
            subject : $scope.subject,
            message : $scope.userCoverLetter,
            attach : $scope.attach
        },
        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    });
});

my php code looks like below

require ('smtp_lib/phpmailer.php');
require ('smtp_lib/smtp.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "yyyyyy";
$mail->FromName = $_POST["name"];
$mail->Subject = $_POST["subject"];
$mail->AddAddress("[email protected]");
$mail->AddReplyTo($_POST["mail"]);
$mail->Body = $_POST["message"].'<br>'.$_POST["no"];
$mail->AddAttachment($_POST["attach"]);
$mail->Send();

if I open php_error_log I getting these errors

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: name in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 16

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: subject in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 17

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 20

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: message in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: name in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: mail in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21

[29-Apr-2015 08:44:36 Europe/Berlin] PHP Notice:  Undefined index: no in C:\xampp\htdocs\wwwroot\contact-form\files\contact_mailer.php on line 21
15
  • your code seems to be right, can you var_dump($_POST) and share the result Commented Apr 29, 2015 at 6:40
  • There is no clue in it. Can you check in your console if ajax request is sent to server and if yes, it is send as a form post like this in example blog.yiiframe.com/wp-content/uploads/2014/11/… Commented Apr 29, 2015 at 6:52
  • Did you test your web service in postman or any other rest client. If you did post response in it your question. Commented Apr 29, 2015 at 6:54
  • @yiiframe i getting success, if success means the data is sent to server right Commented Apr 29, 2015 at 6:56
  • 1
    Remove the extra header and try $post = json_decode(file_get_contents('php://input'), true); var_dump($post); Commented Apr 29, 2015 at 6:56

4 Answers 4

4

This is a known issue Angular with Php you can solve this with;

var form = {name:"x",mail:"x",no:0,subject:"x",message:"x",attach:"x"};
var formData = new form(); 
formData.name = $scope.userName,
formData.mail = $scope.userEmail,
formData.no = $scope.userMobileNo,
formData.subject = $scope.subject,
formData.message = $scope.userCoverLetter,
formData.attach = $scope.attach

$http({
        method: "POST",
        url: "mailer.php",
        data: formData
});

In your php you take this with file_get_contents("php://input") ;

$postdata = file_get_contents("php://input");
$formData = json_decode($postdata);
echo $formData->name;
Sign up to request clarification or add additional context in comments.

3 Comments

I will not say its an issue, its just default content-type set in angular is application/json. you can change them according to your needs and choice
@hurricane I run your code getting error like this Uncaught TypeError: Cannot set property 'name' of undefined
@Selva Actually you need to define a empty object for set elements. You can use formData = new Object(); or create a object.
1
$post = json_decode(file_get_contents('php://input'), true); 

I added this above single line in mailer.php, my problem solved. Thanks for all replies.

Comments

0

First check $scope.userName if it's defined in the console before send the request and you can write it like this:

var data = {
            name : $scope.userName,
            mail : $scope.userEmail,
            no : $scope.userMobileNo,
            subject : $scope.subject,
            message : $scope.userCoverLetter,
            attach : $scope.attach  
          }

$http.post('mailer.php', { params: data }).
  success(function(data, status, headers, config) {
    // something
  }).
  error(function(data, status, headers, config) {
    // something else
  });

Comments

0

Add a directive to set as form post for all your http post request. your php code will remain the same

//Directive - change App with your app name
    App.factory("setAsFormPost", ['$rootScope', function($rootScope) {
        function transformRequest(data, getHeaders) {
            var headers = getHeaders();
            headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";
            return($.param(data));
        }
        return(transformRequest);
    }]);

// you need to pass setAsFormPost to your controller like this
        App.controller('ControllerName',
        ['$scope','setAsFormPost', '$http', function($rootScope, $scope, messageHandler, settings, yiiBox, yiiHttp) {
        $http.post('mailer.php', {name : $scope.userName, mail : $scope.userEmail,
                no : $scope.userMobileNo, subject : $scope.subject, message : $scope.userCoverLetter,
                attach : $scope.attach}, {transformRequest: setAsFormPost}).success(function (result) {
            //something
        }).error(function (error) {
            //something
        });
    }]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.