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

时间:2021-10-05 12:18:26

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. 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:

我正在尝试将索引页面上的联系表单中的消息,姓名和电子邮件发送到我自己的电子邮件中,但我遇到了一些麻烦。我正在使用AngularJS。现在当我点击表单上的提交时,它只是出现了一个加载屏幕,没有其他任何事情发生。这就是我目前所拥有的:

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:

$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):

$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? Any help would be much appreciated! Thanks very much.

php应该与javascript在同一个文件夹中吗?任何帮助将非常感激!非常感谢。

1 个解决方案

#1


0  

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. More about it here

你发出POST ajax请求,所以为了使用你想要传输到PHP应用程序的数据,你需要在$ _POST数组中找到它。更多关于它的信息

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. Just make var_dump($_POST);die(); to debug what's in there and what you need to decode exactly.

您只需要从$ _POST获取数据。只需make var_dump($ _ POST); die();调试那里的内容以及完全解码所需的内容。

#1


0  

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. More about it here

你发出POST ajax请求,所以为了使用你想要传输到PHP应用程序的数据,你需要在$ _POST数组中找到它。更多关于它的信息

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. Just make var_dump($_POST);die(); to debug what's in there and what you need to decode exactly.

您只需要从$ _POST获取数据。只需make var_dump($ _ POST); die();调试那里的内容以及完全解码所需的内容。