Javascript 如何通过jQuery运行php文件向管理员发送电子邮件

Javascript 如何通过jQuery运行php文件向管理员发送电子邮件,javascript,php,jquery,Javascript,Php,Jquery,我需要得到一个通知电子邮件给我的网站管理员,当一个用户提出了一个请求,通过从。我的代码如下,用于链接发送邮件的服务器中的php文件 $("#modelform").submit(function (event) { event.preventDefault(); $.ajax({ url: 'send_mail.php', success: function(){ alert('php runing'); $("

我需要得到一个通知电子邮件给我的网站管理员,当一个用户提出了一个请求,通过从。我的代码如下,用于链接发送邮件的服务器中的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>
然后是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
}
?>

是否存在
send_mail.php
存在?是的,它存在,但什么都没有发生!我放了一个错误日志,它重定向到main.js文件log是uncaughttypeerror:无法读取null“uncaughttypeerror”的属性“offsetWidth”,可能是由modal引起的。你看到警报了吗?没有,只能在控制台中看到!嗯,所以我有很多字段,所以我需要怎么写?var content=(“#contactUs”).val();var smthing=(“#contactUS”).val();$。post('send_mail.php',{content:content,smthing},函数(data){$('#response').html(data);//将返回send_mail.php}的内容;如果我错了,我是对的还是需要做些改变对不起,就像我刚才说的,我知道的很少!字段越多,可以使用
var yourVariableName=$(“#theIdOfYourField”).val()获取它们的值
然后您可以通过将它们添加到
{content:content}
的位置来发布它们。所以它看起来是这样的:
$.post('send_mail.php',{content:content,postObject:jsVar2,etc:etc},函数(data){…
将从php文件访问
postObject
,而
jsVar2
是获取字段值的变量
<?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
}
?>