简体   繁体   English

尝试将数据从有角度的html页面发布到php页面进行处理和发送电子邮件

[英]Trying to post data from an angular html page to a php page for processing and email

I've been working with angular for a short time and I'm trying to get a contact form to submit to a php file at the domain route that's set up to mail us. 我与angular的合作已经有很短的时间了,我正在尝试获取一份联系表格,以设置为邮寄给我们的域名路由提交到php文件。

I've gotten the php to mail us but none of the date is being carried over from the angular application. 我已经将php邮寄给我们,但没有任何日期从Angular应用程序中保留下来。

Here's my code: 这是我的代码:

 angular.module('demoapp') .controller('ContactCtrl', function ($scope, $http, $templateCache) { var method = 'POST'; var url = 'contact.php'; $scope.codeStatus = ''; $scope.add = function() { $http({ method: method, url: url, data: $.param({'data' : FormData}), headers: {'Content-Type': 'application/x-www-form-urlencoded'}, cache: $templateCache }). success(function(response) { $scope.codeStatus = response.data; }). error(function(response) { $scope.codeStatus = response || 'Request failed'; }); return false; }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="contact-form-wrapper"> <h2>Write Us</h2> <form action="contact.php" method="post" enctype="text/plain"> <input class="formtop" type="text" name="name" placeholder="Name (required)"> <input class="formtop" type="text" name="mail" placeholder="Email (required)"> <input class="formlong" type="text" name="subject" placeholder="Subject" size="50"> <textarea class="formarea" name="message" placeholder="Message" size="50"></textarea> <input class="submit-link" type="submit" value="Send"> </form> </div> 

It's posting to this php file: 它发布到此php文件:

<?php

$Subject = $_POST['subject'];
$Name = $_POST['name'];
$Email = $_POST['mail'];
$Message = $_POST['message'];

date_default_timezone_set('America/Los_Angeles');
$ip_address = $_SERVER['REMOTE_ADDR'];
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$date = date ("l, F jS, Y");
$time = date ("h:i A");
$to = "dummy@email.com";
$subject = $Subject;
$msg="
Name: $Name
Email: $Email
Subject: $Subject
Message: $Message
Submitted on: $date $time
IP Address: $ip_address
Host Name: $hostname \n
";

mail($to, $subject, $msg, "From: website@traffikonline.com");
?>

It's probably something very minor that I'm missing, but I've checked a lot of places for answers and this is the closest I've gotten. 我可能想念的东西可能很小,但是我已经在很多地方检查了答案,而这是我得到的最接近的答案。 Grunt and inspector isn't tossing any errors about the jscript and the php file DOES email me (just no data). Grunt和inspector不会向我发送有关jscript和php文件的任何错误的电子邮件(只是没有数据)。

Thanks! 谢谢!

Update your php to : 将您的php更新为:

$data = file_get_contents('php://input');
$json = json_decode($data);
$Subject = $json->subject;
$Name = $json->name;
$Email = $json->mail;
$Message = $json->message;

I ran into this question because I'm currently working on my own Angular + PHP contact form. 我遇到了这个问题,因为我目前正在使用自己的Angular + PHP联系人表单。 Something glaring in your code to me is the total lack of ng declarations. 对我来说,您的代码中显而易见的是ng声明的完全缺乏。

Where is ng-app, ng-controller and ng-model in your html? html中的ng-app,ng-controller和ng-model在哪里? You won't be able to get the data if there's no data binding going on. 如果没有任何数据绑定,您将无法获取数据。 Also, instead of a form action, you should call the "add" function defined in your controller using ng-submit. 另外,应该使用ng-submit调用在控制器中定义的“ add”函数,而不是执行表单操作。

<div ng-app="demoapp" class="contact-form-wrapper">
  <h2>Write Us</h2>
  <form name="form" ng-controller="ContactCtrl" ng-submit="form.$valid && add()" method="post" enctype="text/plain" novalidate>
    <input class="formtop" type="text" name="name" placeholder="Name (required)" ng-model="input.name" required>
    <input class="formtop" ng-model="input.email" type="email" name="mail" placeholder="Email (required)" required>
    <input class="formlong" ng-model="input.subject" type="text" name="subject" placeholder="Subject" size="50">
    <textarea class="formarea" ng-model="input.msg" name="message" placeholder="Message" size="50" required></textarea>
    <input class="submit-link" type="submit" value="Send">
  </form>
</div>

As for the PHP stuff, I'm not really sure. 至于PHP的东西,我不太确定。 But your Angular code is definitely messed up. 但是您的Angular代码肯定搞砸了。

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

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