我有一个使用Ajax进行客户端验证的表单。表格的结尾如下:
$.ajax({ url: 'mail3.php', type: 'POST', data: 'contactName=' + name + '&contactEmail=' + email + '&spam=' + spam, success: function(result) { //console.log(result); $('#results,#errors').remove(); $('#contactWrapper').append('<p id="results">' + result + '</p>'); $('#loading').fadeOut(500, function() { $(this).remove(); }); } });
编辑:这是我的mail3.php文件,处理错误:
$errors=null; if ( ($name == "Name") ) { $errors = $nameError; // no name entered } if ( ($email == "E-mail address") ) { $errors .= $emailError; // no email address entered } if ( !(preg_match($match,$email)) ) { $errors .= $invalidEmailError; // checks validity of email } if ( $spam != "10" ) { $errors .= $spamError; // spam error } if ( !($errors) ) { mail ($to, $subject, $message, $headers); //header ("Location: thankyou.html"); echo "Your message was successfully sent!"; //instead of echoing this message, I want a page redirect to thankyou.html } else { echo "<p id='errors'>"; echo $errors; echo "</p>"; }
我想知道如果ajax请求成功并且没有错误,是否可以将用户重定向到“谢谢”页面。这可能吗?
谢谢!阿米特
当然。只需在成功功能的末尾添加一些内容即可:
if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"
no_errors没有错误时服务器返回响应的位置。
no_errors