PHP不使用AJAX发送电子邮件

时间:2016-08-13 18:30:14

标签: javascript php html ajax email

我正在尝试使用AJAX发送带有php的电子邮件。当我点击按钮时没有任何反应。我的邮箱中没有错误和邮件。这是我的代码:

的index.php:

<div>
    <div class="col-md-offset-4 col-md-4 col-lg-4"> 
        <center>
            <form method="post">
               <section id="left">
                  <label for="form_email">Email</label>
                  <input name="form_email" id="form_email" type="email" >
               </section>
               <section id="right">
                  <label for="form_msg">Message</label>
                  <textarea name="form_msg" id="form_msg"></textarea>
                  <button id="submit" class="button" name="submit" type="button">Send</button>
               </section>
            </form> 
        </center>
    </div>
</div>

这是index.php中的javascript:

<script>
    $("#submit").click(function(){
    var data = {
    email: $("#form_email").val(),
    message: $("#form_msg").val(),
};

$.ajax({
    type: "POST",
    url: "mail.php",
    data: data,
    success: function(){
        $('.success').fadeIn(1000);
    }
    });
        });
</script>

外部mail.php:

<?php
    $email = $_POST['form_email'];
    $subject = "AdrTürkiye Veri Giriş Ekranı";
    $message = $_POST['form_msg'];

    @mail($email, $subject, $message);   
?>

1 个答案:

答案 0 :(得分:0)

jscript

 <script>
    $("#submit").click(function(){
        $.ajax({
            type: "POST",  
            url: "mail.php",
            data:  $('#mailForm').serializeArray(),
            success : function(data){
                alert(data);
            }
        });
    });
    </script> 

mail.php

<?php
    $email = $_POST['form_email'];
    $subject = "AdrTürkiye Veri Giriş Ekranı";
    $message = $_POST['form_msg'];
    $status = @mail($email, $subject, $message); 
    if ($status == 1) {
        echo 1;
    }
    else {
        echo 0;
    }

?>