我想创建一个脚本,当有人点击一个按钮时,将发送一封电子邮件到我们公司的电子邮件。该html将托管在一个静态页面上,并将完成所有客户端,而php代码将托管在另一个域中。使用ajax发送数据,然后发送电子邮件
我到目前为止试过的是。
HTML
<head><title>Report Errors</title>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' type='text/javascript'></script></head>
<script>$(".c").click(function(){
var href = this.href;
$.ajax({
url: 'http://example.com/m35.php',
type: 'POST',
data: { subject: href },
success: function (data) {
setTimeout(function(){
$(".container").html(data)
}, 1000)
}
});
})
</script>
<a href="http://link-to-be-sent.example.com" class="c">send</a>
<div class="container">success</div>
而且在m35.php文件中的代码 -
<?php
$mailTo = "[email protected]";
$mailFrom = "[email protected]";
$subject = $_POST["subject"];
$message = "someone reported the content in subject doubtful.";
mail($mailTo, $subject, $message, "From: ".$mailFrom);
?>
编辑:根据意见,我要澄清的是它甚至不工作的同一域和目录, 但是这种类型的代码也在工作,但如何做到这一点,没有与JavaScript/jquery页面重新加载。
<form method="post" action="http://example.com/m35.php">
<input type="text" name="subject" id="subject" value=""> <input type="submit">
</form>
这是一个CORS的问题,在这里你可以不将数据发送到使用AJAX不同的域。 – Scimonster 2014-09-21 13:02:14
也许这可能有帮助吗? https://stackoverflow.com/questions/17318426/cors-cross-domain-ajax-without-jsonp-by-allowing-origin-on-server – parchment 2014-09-21 13:03:25
感谢您的建议@Scimonster,但这甚至不能在同一个域上工作 – Avadhesh18 2014-09-21 13:06:11