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         = '[email protected]';
    $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, "[email protected]");
?>

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="[email protected]" 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.

2 Answers 2

0

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

Read the http manual here.

Sign up to request clarification or add additional context in comments.

4 Comments

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, as I have not done any contact forms with PHP. That website had the $http.post data set to null.
Interesting, that may work, I'll leave it up to someone who knows Angular better than I.
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?
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.
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         = '[email protected]';
    $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, "[email protected]");
?>

Hope this helps anyone with this problem!

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.