标题(' location:page.html');当ajax提交表单时无法正常工作

时间:2014-10-06 03:50:06

标签: javascript php html forms

我没有收到错误,但是当提交被点击时,表单被清除,电子邮件被发送但没有重定向发生。我也尝试使用脚本重定向,现在好运。

表单页

 <script>
  $(function(){
  $('#form3').submit(function(e){

      //Prevent the default form action
    e.preventDefault();

function validateEmail($email2) {
 var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
 if( !emailReg.test( $email2 ) ) {
    return false;
   } else {
    return true;
  }
}
    var thisForm = $(this);

    $("#emailError2").hide();
    $("#firstnameError2").hide();
    $("#lastnameError2").hide();
    $("#cityError2").hide();
    $("#phoneError2").hide();
    $("#countryError2").hide();
    $("#messageError2").hide();

    var errors = false;

    var email2 = $("input[id='email2']").val();
    var firstname2 = $("input[id='firstnam2e']").val();
    var lastname2 = $("input[id='lastname2']").val();
    var phone2 = $("input[id='phone2']").val();
    var business = $("input[id='business']").val();
    var nbusiness = $("input[id='nbusiness']").val();
    var message2 = $("textarea[id='message2']").val();



    if(( !validateEmail(email2)) || (email2=="")) {
     errors = true; 
     $("#emailError2").fadeIn();

     return;

    }

    if(firstname2=="") {
     errors = true; 
     $("#firstnameError2").fadeIn();
     return;

    }

    if(lastname2=="") {
     errors = true; 
     $("#lastnameError2").fadeIn();
     return;

    }

    if(business=="") {
     errors = true; 
     $("#cityError2").fadeIn();
     return;

    }

     if(phone2=="") {
     errors = true; 
     $("#phoneError2").fadeIn();
     return;

    }

    if(errors == false){

    //Hide the form
    $(this).fadeOut(function(){
      //Display the "loading" message
      $("#loading2").fadeIn(function(){
        //Post the form to the send script
        $.ajax({
          type: 'POST',
          url: thisForm.attr("action"),
          data: thisForm.serialize(),
          //Wait for a successful response
          success: function(data){
            //Hide the "loading" message
            $("#loading2").fadeOut(function(){
              //Display the "success" message
              $("#success2").text(data).fadeIn();
            });
          }
        });
      });
    });

    }

  })
});

我在这里有一个完整的输入形式输入,然后是:

<div class="contactRow" style="height:160px; margin-top:91px;">
<textarea name='message2' id="message2" rows="8" cols="38" placeholder="Comments"></textarea>   </div>
 <div class="contactRow"><center><input type="checkbox" name="I-agree" required>I agree to the<a   href="http://www.-------------.com/wp/terms-conditions" target="_blank">Terms and Conditons</a></input>  </center><div class="contactSubmit"><input type='submit' value="Submit"></div></div>

seperate.php页面发送电子邮件

<?php
if (isset($_POST['email2']))
//if "email" is filled out, send email
{
//send email
$email2 = $_POST['email2'] ;
$firstname2 = $_POST['firstname2'] ;
$lastname2 = $_POST['lastname2'] ;
$nbusiness = $_POST['nbusiness'] ;
$phone2 = $_POST['phone2'] ;
$business = $_POST['business'] ;
$message2 = $_POST['message2'] ;

$sizeof2 = $_POST['sizeof2'] ;
$whattype2 = $_POST['whattype2'] ;
$whoare2 = $_POST['whoare2'] ;
$wheredid2 = $_POST['wheredid2'] ;

$comment2 = "$message2 \n\n $firstname2 \n $lastname2 \n $phone2 \n $business \n $nbusiness \n     $email2 \n $sizeof2 \n $whattype2 \n $whoare2  \n $wheredid2 \n\n";

 /*$headers = 'From: ------@-----.com' . "\r\n" .
'Reply-To: ------a@s-----.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();*/

$headers2 = 'From: '.$email2."\r\n" .
    'Reply-To: '.$email2."\r\n" .
    'X-Mailer: PHP/' . phpversion();

//-----@-----.com
mail("------@-----.ca",  "-------------: ".$firstname2, $comment2, $headers2);
header ('Location: thankyou.html');
exit ();
}else{
echo "There was a problem with the registration";
}
?>

1 个答案:

答案 0 :(得分:0)

由于您的表单提交会导致AJAX请求,因此您应该在成功完成该请求后重定向。

$.ajax({
  type: 'POST',
  ...
  // Wait for a successful response
  success: function(data) {
      ...
      window.location.href = "thankyou.html"; // <-- REDIRECT HERE
  }
});

如果您不希望将原始页面放入会话历史记录中,也可以使用window.location.replace("thankyou.html");

在你的php中你可能应该用代码返回header ('Location: thankyou.html');替换消息或任何你想要的AJAX请求(例如echo "Success!";)。