简体   繁体   English

联络表单回应非常缓慢(PHP / jQuery / Ajax)

[英]Very slow contact form response (PHP/jQuery/Ajax)

I'm setting up an extremely basic PHP/jQuery/Ajax contact form; 我正在建立一个非常基本的PHP / jQuery / Ajax联系表格; 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. 并且在工作时,响应速度非常慢(成功消息显示后需要5秒以上的时间),我想知道自己可能做错了什么。

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:

<?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 .= "\n";

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

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

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

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

?>

jQuery: 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. 感谢您的协助。

Did you try to comment out 您是否尝试发表评论
$success = mail($EmailTo, $Subject, $Body, "From:".$email); ?

I might be wrong, but, sending email might take a few seconds 我可能是错的,但是发送电子邮件可能需要几秒钟
i suggest you to send email using other ajax request. 我建议您使用其他Ajax请求发送电子邮件。


Because PHP is not async language, at least by default . 因为PHP不是异步语言, 至少默认情况下是这样

  1. First PHP script: receive datas from JS, and respond with 200(success) 第一个PHP脚本: receive datas from JS, and respond with 200(success)
    jQuery will trigger 'success' event before sending Email. jQuery将在发送电子邮件之前触发“成功”事件。
    this will not block or bother you and other users. 这不会阻止或打扰您和其他用户。

  2. Second PHP script: just to send Email, without modal and etc. 第二个PHP脚本:仅发送电子邮件,不带模式等。


   $.ajax({
        //here, you could send some infos to  use with DB, check if
       //datas, e-mail is valid with filter_var($email, FILTER_VALIDATE_EMAIL), etc..
        type: $this.attr('method'),
        url: $this.attr('action'),
        data: $this.serialize(), 
        success : function(text){
            if (text == "success"){
                formSuccess();
            }
        }
    });
   function formSuccess(){
       $("#msg-submit").removeClass("hidden");
       $.ajax({
        //here, you could send some infos to build and send Email
        //It's right, because the first ajax did succeed right ?
        type: $this.attr('method'),
        url: $this.attr('action'),
        data: $this.serialize(), 
        success : function(text){
            //yey :D
        }
   }

I'm going to assume you're assuming PHPMailer , but correct me if I'm wrong. 我假设您使用的是PHPMailer ,但是如果我错了,请更正我。 If not, consider using that to implement this next part. 如果没有,请考虑使用它来实现下一部分。

Your PHP script is waiting for the email to be sent. 您的PHP脚本正在等待发送电子邮件。 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 . 查看有关如何使用PHPMailer做到这一点的出色答案

@nickpish @nickpish

you have php , jquery and ajax . 您有phpjqueryajax the issue with the bottleneck can either be the front-end or the backend. 瓶颈的问题可能是前端还是后端。 you need a powerful tool in order to debug the issue. 您需要强大的工具来调试问题。 if you have an error in your jquery there is no way to figure that out. 如果您的jquery有错误,则jquery in addition, if you have a backend issue, that can cause your jquery to take more time in responding back to your front-end ui. 此外,如果您遇到后端问题,则可能导致jquery花费更多时间来响应前端ui。 if you look at the image below 如果您看下面的图片

在此处输入图片说明

you need to use something like firefox, (right click anywhere on your ui and select [ Inspect Element Q ] click on the net panel and choose XHR . in the console window you'll see if there are any errors; furthermore, you'll notice your back-end php file and it'll give you a response time. 您需要使用firefox之类的东西(在用户界面的任何位置上单击鼠标右键,然后选择[ Inspect Element Q ],然后在网络面板上单击并选择XHR 。在控制台窗口中,您会看到是否有任何错误;此外,您还会注意您的后端php文件,它将为您提供响应时间。

if the back-end script is too slow, you can go into your back-end file. 如果后端脚本太慢,则可以进入后端文件。 you have a mail function, maybe it's the server that's taking too long. 您具有邮件功能,可能是服务器花费的时间太长。 you may need to switch out your smtp to something faster or leave it as is. 您可能需要更快地将smtp切换为其他内容或保持原样。 if you choose to stay with your current smtp that may take time, you should think of putting a progress window meanwhile. 如果您选择保留可能需要花费一些时间的当前smtp ,则应考虑同时放置一个进度窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM