我需要得到一个通知电子邮件到我的网站管理员,当一个用户通过一个请求。我的代码如下,链接PHP文件在我的服务器发送邮件
$("#modelform").submit(function (event) {
event.preventDefault();
$.ajax({
url: 'send_mail.php',
success: function(){
alert('php runing');
$("#sendRequest").modal("show");
$("#myModal").modal("toggle");
}
});
});
但它没有反应!我的知识有点少,有人能指导我做到这一点吗?我检查了这个问题,这是错误的方式,我做或我需要链接任何文件以外的引导库?
您可以尝试这样做:
HTML:
<textarea id="contactUs"></textarea><div id="button">Send</div>
<div id="response"></div>
jQuery: $("#button").click(function(){ //when div id="button" is clicked
var content = $("#contactUs").val(); //get value of textarea id="contactUs"
$.post('send_mail.php',{content: content}, function(data){ //post data
$('#response').html(data); //return content of send_mail.php
});
});
send_mail.php:
<?php
if(isset($_POST['content']) === true){
$content = $_POST['content']; //might wanna sanitize if you're storing into db
$to = "YourEmail@example.com"; //The email sending to
$subject = "Sent From Contact form"; //The subject of email
mail($to, $subject, $content, 'From: contact@example.com'); //PHP mail() function
echo "Sent!"; //This will go to div id="response" on success
} else {
echo "Error!"; //This will go to div id="response" on error
}
?>