简体   繁体   English

用PHP在angular JS中实现$ http.post

[英]Implementing $http.post in angular JS with PHP

I'm trying to send the message, name and email from a contact form on my index page to my own email but am having some troubles. 我正在尝试将索引页面上的联系表中的消息,名称和电子邮件发送到我自己的电子邮件中,但是遇到了一些麻烦。 I'm using AngularJS. 我正在使用AngularJS。 Right now when I click submit on the form, it just comes up with a loading screen and nothing else ever happens. 现在,当我单击表单上的“提交”时,它只显示了一个加载屏幕,什么都没有发生。 This is what I currently have: 这是我目前拥有的:

HTML: HTML:

<form ng-submit="save()" class="contactForm" name="form" ng-hide="loaded" ng-controller="formCtrl">
            <input class="input" required="required" type="text" name="name" placeholder="Your Name" ng-model="message.name" />
            <input class="input email" required="required" type="email" name="email" value="" placeholder="Your Email" ng-model="message.email" /><br />
            <textarea class="textarea" rows="5" required="required" placeholder="Your Message" ng-model="message.text" ></textarea>
            <button class="btn green">Send Message</button>
        </form>

JS: JS:

$scope.save = function () {
      $scope.loaded = true;
      $scope.process = true;
      $http.post('sendemail.php', $scope.message).success(function () {
          $scope.success = true;
          $scope.process = false;
      });
    };

PHP (sendemail.php): PHP(sendemail.php):

$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);

$email = $request->message.email;
$name = $request->message.name;
$message = $request->message.text;

$from="From: $name<$email>\r\nReturn-path: $email"; 
$subject="Message sent using your contact form"; 
mail("myemail@gmail.com", $subject, $message, $from); 

I've also tried the following php: 我也尝试过以下php:

var_dump($_POST);die();
$_POST = json_decode($rest_json, true);

$email = $_POST["message.email"];             //$request->message.email;
$name = $_POST["message.name"];                     //$request->message.name;
$message = $_POST["message.text"];          //$request->message.text;

$from="From: $name<$email>\r\nReturn-path: $email"; 
$subject="Message sent using your contact form"; 
mail("myemail@gmail.com", $subject, $message, $from);

Should the php be in the same folder as the javascript? php应该和javascript放在同一个文件夹中吗? Any help would be much appreciated! 任何帮助将非常感激! Thanks very much. 非常感谢。

You making POST ajax request, so in order to use data that you're trying to transfer to your PHP application, you need to find it in $_POST array. 您发出POST ajax请求,因此,为了使用要尝试传输到PHP应用程序的数据,您需要在$ _POST数组中找到它。 More about it here 在这里了解更多

More specifically you don't need this line $rest_json = file_get_contents("php://input"); 更具体地说,您不需要此行$rest_json = file_get_contents("php://input");

You just need to get data from $_POST . 您只需要从$_POST获取数据。 Just make var_dump($_POST);die(); 只需使var_dump($_POST);die(); to debug what's in there and what you need to decode exactly. 调试其中的内容以及您需要准确解码的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM