doulao7572 2014-01-20 09:29 采纳率: 0%
浏览 16

Ajax联系表不发送电子邮件[重复]

This question is an exact duplicate of:

Sorry for being a noob but I'm trying my best. I've done and read everything I could find and I have never got this to work. Would really appreciate the help. The form id matches in the html and javascript. The PHP is linked in the Javascript and I have the javascript linked in the head of my html. What am I missing? I've tried other codes I found online as well and nothing.. The issue is that no email ever gets sent through. If you hit send the page reloads and thats it.

<!--[if lte IE 8]>
<script src="js/html5shiv.js"></script><![endif]-->

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.dropotron.js"></script>
<script src="js/skel.min.js"></script>
<script src="js/skel-panels.min.js"></script>
<script src="js/init.js"></script>
<script src="js/contact.js"></script>
<noscript>
    <link rel="stylesheet" href="css/skel-noscript.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/style-noscript.css" />
</noscript>


<!-- Contact Form-->
<div class="content style4 featured">
<div class="container small">
    <form id="contact" form method="post">

    <div class="row half">
        <div class="6u"><input type="text" class="text" name="name" placeholder="Name" /></div>
        <div class="6u"><input type="text" class="text" placeholder="Email" name="email" /></div>
    </div>

    <div class="row half">
        <div class="12u"><textarea name="text" placeholder="Message"></textarea></div>
    </div>

    <div class="row">
        <div class="12u">
            <ul class="actions">
                <li><input type="submit" class="button" value="Send Message" /></li>
                <li><input type="reset" class="button alt" value="Clear Form" /></li>
<p class="success" style="display:none">Your message has been sent successfully.</p>
                <p class="error" style="display:none">E-mail must be valid and message must be longer than 100 characters.</p>
            </ul>
        </div>

    </div>

    </form>
</div>
</div>

PHP:

<?php
    // Email Submit
    // Note: filter_var() requires PHP >= 5.2.0
    if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text']) &&      filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
        // detect & prevent header injections
        $test = "/(content-type|bcc:|cc:|to:)/i";
        foreach ( $_POST as $key => $val ) {
            if (preg_match( $test, $val ))
            exit;
        }

        //send email
        mail( "test@gmail.com", "Contact Form: ".$_POST['name'], $_POST['text'], "From:" .      $_POST['email'] );
    }
?>

JS:

$('#contact').submit(function() {
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();
    var dataString = 'name=' + name + '&email=' + email + '&message=' + message;

    $.ajax({
        type : "POST",
        url : "mail.php",
        data : dataString,
        cache : false,
        success : function() {              
            $("#contact").fadeOut(300);
                                $("#notice").fadeIn(400);
        }
    });
    return false;
});

Thanks.

</div>
  • 写回答

1条回答 默认 最新

  • drlhsfqoa350437979 2014-01-20 09:33
    关注

    try preventing the default form submission by changing:

    $('#contact').submit(function() {
    ...
    

    to

    $('#contact').submit(function(evt) {
        evt.preventDefault(); 
        //rest of your js code
    

    and you dont have input elements with id name, email and message as per your posted code. Add those as ids to your input elements

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数