0

我有一个简单的表格,您可以在其中输入电子邮件然后提交。表单通过 jquery 发送到 php 脚本,该脚本发送电子邮件(如果电子邮件有效)返回 JSON 响应,然后 jquery 根据响应在 div 中显示成功或失败消息。得到了所有的工作。

我想要做的是清除表单,如果它被验证为成功发送。我可以清除表单字段并毫无问题地显示成功/失败消息,但发现了一个错误。如果有人在表单中输入了另一封电子邮件,则不会出现成功(或失败)消息。但是,php 仍在正确执行数据(发送电子邮件)。仅缺少成功/失败消息。成功/失败消息在您第一次提交表单时有效,但在重置后无效。这是表格:

<form action="" id="emailsend" method="post" >
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Submit" ; />
</form>

出现成功/失败消息的 div:

<div id="formResponse"></div> 

jQuery

$(document).ready(function(){  

$("#emailsend").submit(function(){  

    $.ajax({  
        type: "POST",  
        url: "sendunit4.php",  
        data: $("#emailsend").serialize(),  
        dataType: "json",  

        success: function(msg){  
            $("#formResponse").removeClass('error');  
            $("#formResponse").removeClass('success');  
            $("#formResponse").addClass(msg.status);  
            $("#formResponse").html(msg.message);
            $('#formResponse').fadeOut(2000);
            $('#emailsend')[0].reset();



        },  
        error: function(){  
            $("#formResponse").removeClass('success');  
            $("#formResponse").addClass('error');  
            $("#formResponse").html("There was an error submitting the form. Please try again.");  
        }  
    });  

    return false;  

});  

});  

PHP 脚本的相关部分(sendunit4.php):

$to = $_POST['email'];
if ($recipients = $swift->send($message, $failures))
{
$response_array['status'] = 'success';  
$response_array['message'] = 'Email sent!';
}
else{
$response_array['status'] = 'error';  
$response_array['message'] = 'There was a problem sending';  
}
echo json_encode($response_array); 

任何想法,将不胜感激。谢谢!

4

0 回答 0