0

I am working on getting a contact form set up on my website, but cannot seem to get the PHP working. I am able to get the email to send to my email, but everything is empty that I would get from the $_POST method. I am very new to PHP, but am quite experienced with AngularJS. Any help would be appreciated.

PHP:

<?php
    $to         = 'name@domain.com';
    $subject    = $_POST["subject"];
    $message    = $_POST["message"];
    $fromEmail  = $_POST["email"];
    $fromName   = $_POST["name"];
    $headers    = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();

    mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>

HTML:

<form name="contactForm" ng-submit="submit()" novalidate>
    <div class="row">
        <div class="col-md-6" ng-class="{'has-error' : contactForm.firstName.$invalid && !contactForm.firstName.$pristine}">
            <label for="firstName">First Name:</label>
            <input type="text" placeholder="Joe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="firstName" ng-model="firstName" required />
        </div>
        <div class="col-md-6" ng-class="{'has-error' : contactForm.lastName.$invalid && !contactForm.lastName.$pristine}">
            <label for="lastName">Last Name:</label>
            <input type="text" placeholder="Schmoe" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="lastName" ng-model="lastName" required />
        </div>
    </div>
    <div class="row">
        <div class="col-md-12" ng-class="{'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine}">
            <label for="email">Email Address:</label>
            <input type="email" placeholder="jSchmoe@joeschmoe.gov" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="email" ng-model="email" required />
        </div>
    </div>
    <div class="row">
        <div class="col-md-12" ng-class="{'has-error' : contactForm.subject.$invalid && !contactForm.subject.$pristine}">
            <label for="subject">Subject:</label>
            <input type="subject" placeholder="Help me!" class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="subject" ng-model="subject" required />
        </div>
    </div>
    <div class="row">
        <div class="col-md-12" ng-class="{'has-error' : contactForm.body.$invalid && !contactForm.body.$pristine}">
            <label for="body">Body:</label>
            <textarea style="width: 100%; height: 173px; resize: none;" type="subject" placeholder="I am stuck inside this website and want out!  I have been here for years and years..." class="form-control ng-pristine ng-invalid ng-invalid-required ng-valid-minlength" name="body" ng-model="body" required />
        </div>
    </div>
    <div class="row">
        <div class="col-md-offset-2 col-md-8">
            <br />
            <button type="submit" class="btn btn-success col-md-12" ng-disabled="contactForm.$invalid">
                <h4>Send me an email!</h4>
            </button>
        </div>
    </div>
</form>

JS:

window.dp.controller('ContactController', ['$scope', '$http', function($scope, $http) {
    $scope.firstName = new String();
    $scope.lastName = new String();
    $scope.email = new String();
    $scope.subject = new String();
    $scope.body = new String();

    $scope.submit = function() {
        $scope.fullName = $scope.firstName + ' ' + $scope.lastName;

        $scope.config = {
            params: {
                name: $scope.fullName,
                email: $scope.email,
                subject: $scope.subject,
                message: $scope.body
            }
        };

        $http.post("/php/contact.php", null, $scope.config)
            .success(function(result){
                // show success msg
            }).error(function(result){
                // show error msg
            });
        //
    };
}]);

I have looked into this problem a good bit, but most of the fixes were adding in the name attribute to the HTML Input tags. I did look at this answer, but it did not seem to work for me.

EDIT:

Just found an error log for contact.php with the following contents:

[20-Apr-2015 02:03:07 UTC] PHP Notice:  Undefined index: subject in /home/[mydomainname]/public_html/dp/php/contact.php on line 3
[20-Apr-2015 02:03:07 UTC] PHP Notice:  Undefined index: message in /home/[mydomainname]/public_html/dp/php/contact.php on line 4
[20-Apr-2015 02:03:07 UTC] PHP Notice:  Undefined index: email in /home/[mydomainname]/public_html/dp/php/contact.php on line 5
[20-Apr-2015 02:03:07 UTC] PHP Notice:  Undefined index: name in /home/[mydomainname]/public_html/dp/php/contact.php on line 6

I replace the part of the path in the above log with [mydomainname] to hide what the domain that this is on, and also shortened the log, as it was these 4 lines repeated for every time I tried to submit an email.

I have also directly tried hitting this file with a set of example params but I had the same result as before, which is making me thing something weird is going on between the browser making the request, and the file contact.php receiving the request.

Community
  • 1
  • 1
Burn_E99
  • 1,098
  • 1
  • 17
  • 27

2 Answers2

0

Your second parameter to $http.post should be the data but you have null.

Read the http manual here.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • I tried adding in `$scope.config` and `$scope.config.params` in place of null, but am still getting the same result. Only difference was when I set the data equal to `$scope.config` the email did not even get sent. The reason that I placed null there was because I was following off of an example from [here](http://www.angularjshub.com/examples/forms/formsubmission/), as I have not done any contact forms with PHP. That website had the `$http.post` data set to null. – Burn_E99 Apr 20 '15 at 01:39
  • Interesting, that may work, I'll leave it up to someone who knows Angular better than I. – Devon Bessemer Apr 20 '15 at 01:43
  • Alright. Thanks Devon. I do see where you were coming from with the missing data parameter. Do you know anything about PHP, or see anything wrong with my PHP script? – Burn_E99 Apr 20 '15 at 01:47
  • Your PHP looks pretty straight forward, I don't see any issue sticking out. Test that independently and see what happens. HTTP Request addons for browsers come in handy in that regard. – Devon Bessemer Apr 20 '15 at 01:49
0

So I found out what I was doing wrong. Apparently, if you want to access the PARAMETERS of the HTTP POST request, you must use $_REQUEST, whereas if you want to access the DATA of the HTTP POST request, you must use $_POST.

My new PHP is as follows:

<?php
    $to         = 'name@domain.com';
    $subject    = $_REQUEST["subject"];
    $message    = $_REQUEST["message"];
    $fromEmail  = $_REQUEST["email"];
    $fromName   = $_REQUEST["name"];
    $headers    = 'From: '.$fromName.' <'.$fromEmail.'>'."\r\n".'Reply-To: '.$fromEmail."\r\n".'X-Mailer: PHP/'.phpversion();

    mail($to, $subject, $message, $headers, "-femail.name@domain.com");
?>

Hope this helps anyone with this problem!

Burn_E99
  • 1,098
  • 1
  • 17
  • 27