dqpd4268 2015-05-25 03: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-25 03: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
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?