douyingmou1389 2016-08-03 20:09
浏览 56
已采纳

非常慢的联系表单响应(PHP / jQuery / Ajax)

I'm setting up an extremely basic PHP/jQuery/Ajax contact form; and while it is working, the response is extremely slow (5+ seconds for the success message to show) and I'm wondering what I might be doing wrong.

Here is all the relevant code:

Markup:

<form id="contact-form" name="contact-form" action="php/form-process.php" method="post" role="form">
    <input type="text" name="name" id="name" placeholder="Name" required>
    <input type="email" name="email" id="email" placeholder="Email" required>
    <textarea id="message" name="message" rows="5" placeholder="Your message" required></textarea>
    <button type="submit" id="form-submit" class="contact-button">Submit</button>
    <div id="msg-submit" class="hidden">Message sumbitted!</div>
</form>

PHP:

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$EmailTo = "test@test.com";
$Subject = "New Contact Form Message";

// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "
";

$Body .= "Email: ";
$Body .= $email;
$Body .= "
";

$Body .= "Message: ";
$Body .= $message;
$Body .= "
";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success){
   echo "success";
}else{
    echo "invalid";
}

?>

jQuery:

$('#contact-form').submit(function(event){
        event.preventDefault();
        submitForm(); 
    });

    function submitForm(){
        var $this = $('#contact-form');
        $.ajax({
            type: $this.attr('method'),
            url: $this.attr('action'),
            data: $this.serialize(),
            success : function(text){
                if (text == "success"){
                    formSuccess();
                }
            }
        });
    }
    function formSuccess(){
        $("#msg-submit").removeClass("hidden");
    }

I'm leaving out all form validation functionality to stick to the absolute basics. I'm not sure if it's the server causing the slow response, but ideally, I'd like the success message to show as soon as the user clicks submit. Thanks for any assistance here.

  • 写回答

3条回答 默认 最新

  • dsy48837 2016-08-03 20:13
    关注

    I'm going to assume you're assuming PHPMailer, but correct me if I'm wrong. If not, consider using that to implement this next part.

    Your PHP script is waiting for the email to be sent. That's not how it should be done. You need to send it using a mail server. See this superb answer on how to do that using PHPMailer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!