我找到了一个关于使用$http
服务向PHP发送数据的教程here。
我想我理解Angular中提交的语法,但我不明白他是如何使用$data = file_get_contents("php://input");
获取变量的。它似乎是在给file_get_contents
提供一个随机网址?
无论如何,这是我得到的。这是为了在不刷新的情况下发送电子邮件。我现在对退回任何东西不感兴趣,我只是想发送电子邮件。
HTML:
<script type="text/javascript" src="http://allenhundley.com/js/contact/contact.js"></script>
<div id="format">
<div id="header">
</div>
<p id="text">
Have any questions? Have a project? Shoot me an email here or at Allen@AllenHundley.com.
</p>
<br />
<input class="input" type="email" ng-model="email" placeholder="Email"/>
<input class="input" type="text" ng-model="subject" placeholder="Subject"/>
<textarea id="message" ng-model="message" placeholder="Message"></textarea>
<button id="send" name="send" ng-click="emailCtrl.send()">Send Email</button>
</div>
AngularJS:
var emailController = spa.controller("emailController", ["$scope", "$http", function($scope, $http) {
this.send = function() {
$http.post('/php/send_email.php', {
email : $scope.email,
subject : $scope.subject,
message : $scope.message
});
};
});
PHP:
<?php
$user_email = $_POST["email"];
$user_subject = $_POST["subject"];
$user_message_input = $_POST["message"];
$user_headers = 'MIME-Version: 1.0\r\n';
$user_headers .= 'Content-type:text/html;charset=UTF-8\r\n';
$user_headers .= 'From: <noReply@AllenHundley.com>\r\n';
$user_message = "
<html>
<head>
<title>
Thanks for contacting me!
</title>
</head>
<body>
" . $user_message_input . "
</body>
</html>
";
mail($user_email, $user_subject, $user_message, $user_headers);
$developer_to = "Allen@AllenHundley.com";
$developer_subject = $user_subject;
$developer_message = $user_message_input;
$developer_headers = 'MIME-Version: 1.0\r\n';
$developer_headers .= 'Content-type:text/html;charset=UTF-8\r\n';
$developer_headers .= 'From: <' . $user_email . '>\r\n';
$user_message = "
<html>
<head>
<title>
Thanks for contacting me!
</title>
</head>
<body>
" . $developer_message . "
</body>
</html>
";
mail($developer_to, $developer_subject, $developer_message, $developer_headers);
?>
我试过阅读$http
上的文档,但我认为我已经有了这个部分。它没有说任何关于PHP的内容。
我从哪里开始?
答案 0 :(得分:2)
使用默认的角度设置,您需要读取<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form id="contact-form" name="contact-form" class="contact-form">
<fieldset>
<div class="row">
<div class="col-lg-12 form-group">
<h2>Sign In</h2>
<label for="user">Username</label>
<input class="form-control" type="text" value="" name="user" placeholder="Username" aria-required="true" />
</div>
<div class="col-lg-12 form-group">
<label for="Password">Password</label>
<input class="fom-control" type="text" value="" name="password" placeholder="Password" aria-required="true" />
</div>
<div class="col-lg-12 form-group">
<input type="submit" class="btn btn-submit" />
</div>
</div>
<!--end row-->
</fieldset>
</form>
</div>
的输入,因为angular不会以php://input
格式发送数据。它以application/x-www-form-urlencoded
格式将其发送到服务器。
您有两种选择:
application\json
功能中设置headers
选项,如下所示:
$http
&#13;
$http({
method: 'POST',
url: '/php/send_email.php',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
email : $scope.email,
subject : $scope.subject,
message : $scope.message
}
});
代替php://input
。您可以执行类似$_POST
的操作,然后执行$post = json_decode(file_get_contents('php://input'));
等操作。第二个想法,除了更改标题之外,还有一个选项。您还必须转换数据。 This post有一个如何做到这一点的好例子。它还会显示另一个选项,即更改默认的内容类型标题,并将其作为整个模块的默认行为。
以下是该博客文章中提供的将数据转换为url编码字符串的函数:
$user_email = $post['email'];
&#13;
如果您使用上面列出的第一个选项,则只需将JS对象传递给var param = function(obj) {
var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
for(name in obj) {
value = obj[name];
if(value instanceof Array) {
for(i=0; i<value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if(value instanceof Object) {
for(subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if(value !== undefined && value !== null)
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
return query.length ? query.substr(0, query.length - 1) : query;
};
函数,然后再将其分配给params()
属性:
data