dqpd4268 2015-05-24 19:24
浏览 46
已采纳

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

i need to get a notification email to my site admin, when one user made a request through a from. my code is as follows, to link the php file in my server which sends the mail

$("#modelform").submit(function (event) {
        event.preventDefault();
        $.ajax({
    url: 'send_mail.php',
    success: function(){
         alert('php runing');
         $("#sendRequest").modal("show");
         $("#myModal").modal("toggle");
    }
});

    });

but it doesn't react! my knowledge is little low can anyone guide me to achieve this? i checked this question is this wrong the way i do or do i need to link any file other than bootstrap libraries?

  • 写回答

1条回答 默认 最新

  • duankuaizhe8257 2015-05-24 19:40
    关注

    You can try something like this:


    The HTML:

    <textarea id="contactUs"></textarea><div id="button">Send</div>
    <div id="response"></div>
    

    The 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
        });
    });
    

    Then the 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
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部