使用GoDaddy Shared Hosting从AngularJS表单发送时,PHP $ _POST为空

时间:2015-04-20 01:24:14

标签: php angularjs html-form-post

我正在努力在我的网站上设置联系表单,但似乎无法让PHP正常工作。我能够收到要发送到我的电子邮件的电子邮件,但是我从$ _POST方法获得的所有内容都是空的。我是PHP的新手,但我对AngularJS很有经验。任何帮助将不胜感激。

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
            });
        //
    };
}]);

我已经很好地研究了这个问题,但大多数修复都在name属性中添加了HTML Input标记。我确实看了this回答,但它似乎对我不起作用。

修改

刚刚找到contact.php的错误日志,其中包含以下内容:

[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

我用[mydomainname]替换上面日志中路径的一部分来隐藏它所在的域,并且还缩短了日志,因为每次我尝试提交电子邮件时重复这四行

我还直接尝试使用一组示例params命中这个文件,但是我得到了和以前一样的结果,这让我觉得在发出请求的浏览器和文件contact.php接收之间发生了一些奇怪的事情请求。

2 个答案:

答案 0 :(得分:0)

$http.post的第二个参数应该是数据,但您有null

阅读http手册here

答案 1 :(得分:0)

所以我发现了我做错了什么。显然,如果要访问HTTP POST请求的PARAMETERS,则必须使用$_REQUEST,而如果要访问HTTP POST请求的DATA,则必须使用$_POST

我的新PHP如下:

<?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");
?>

希望这有助于解决这个问题的任何人!