【问题标题】:Execute window.location redirect AFTER ajax is completeajax 完成后执行 window.location 重定向
【发布时间】:2013-12-03 06:01:33
【问题描述】:

总之,我有一个通过 AJAX 加载的表单。此表单的必填字段通过另一个 AJAX 调用进行检查。填写完所有必填字段后,将向 db 添加一条新记录,现在应该将用户发送到我的付款页面以完成申请流程。我尝试添加window.location = "http://www.paymentPage.php";,但无论我将这段代码放在哪里,它都会与我的ajax 代码一起执行。我什至尝试在validMA.php 中的代码末尾添加die(header:location('paymentPage.php'));。我知道我遗漏了一些简单的东西……只是想不通。

PHP 代码:

<?php 
session_start();
include('header.htm');?>
</head> 
<body> 
<div id="container">
   <div id="content"><div class="content">
      <h1>New Member Application</h1>
   <form method="post" name="checkUserMA" id="checkUserMA">
    <label class="clear" style="width:120px">Username/Email<br><span class="small"></span></label>
    <input type="text" name="usernameMA" id="usernameMA" class="green" style="width:300px;"/><br><br>
    <input type="submit" id="checkUserMA" class="submit" value="Submit" />
</form>
    <div id="errorMA"></div>
    <div id="resultMA"></div>
    </div></div><!--end content-->  
<div id="footer">
<?php include("footer.htm") ?>
<!--<?php include("disclaimer.htm") ?>-->
</div><!--end footer-->
<div class="clear"></div>
</div><!--end container-->
<div class="clear"></div>
</body> 
</html> 

JS代码:

$(document).ready(function() {

    //NEW MEMBER APPLICATION
    $('#resultMA').hide();
    $('#errorMA').hide();
    $("#checkUserMA").submit(function(event){
        event.preventDefault();
        $("#resultMA").html('');
        var values = $(this).serialize();
        $.ajax({
            url: "checkMA.php",
            type: "post",
            data: values,
            success: function(result){
                $("#resultMA").html(result).fadeIn();
                $('.error').hide();
                validMA();
                window.location = "http://184.154.174.162/~nsgpcom/memberAppPay.php";
            },
            error:function(){
               $("#resultMA").html('There was an error.').fadeIn();
            }
        });//end ajax
    });
function validMA(){
    $("#fullFormMA").click(function(e){
        e.preventDefault();
        $("#errorMA").html('');
        var fullForm = $(this).serialize();
        $.ajax({
            url: "validMA.php",
            type: "post",
            data: fullForm,
            success: function(result){
                $("#errorMA").html(result).fadeIn();
            },
            error:function(){
               $("#errorMA").html('There was an error.  Please try again.').fadeIn();
            }
        });//end 
    });
    $("errorMA").hide();
}//end function

validMA.php 代码:

<?php
session_start();
include('functions.php');
connect();
$firstName = protect($_POST['firstName']); 
$lastName = protect($_POST['lastName']);
$address1 = protect($_POST['address1']);
$address2 = protect($_POST['address2']);
$city = protect($_POST['city']);
$state = protect(strtoupper($_POST['state']));
$zip = protect($_POST['zip']);
$required = array('firstName','lastName','address1','city','state','zip');
    // Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
  }
}
if ($error){
    echo "You need to fill in all required (*) fields";
} else {
    $sql2 = "INSERT INTO newMembers (username,firstName,lastName,address1,address2,city,state,zip,publicEmail,workPhone,privateAddress1,privateAddress2,privateCity,privateState,privateZip,homePhone,cellPhone,website,facebook,twitter,linkedIn,education,licensure,certification,statement,dateAdded) VALUES ('$username','$firstName','$lastName','$address1','$address2','$city','$state','$zip','$publicEmail','$workPhone','$privateAddress1','$privateAddress2','$privateCity','$privateState','$privateZip','$homePhone','$cellPhone','$website','$facebook','$twitter','$linkedIn','$education','$licensure','$certification','$statement','$dateAdded')";
    $result2 = mysql_query($sql2) or die(mysql_error());
}?>

希望理解这个逻辑。我知道我会一次又一次地使用它。

【问题讨论】:

  • 您还可以打印出您的 ajax 调用返回的错误。错误:函数(xhr,状态,错误){ console.log(错误+“此处错误”); } 并且应该是 window.location.href = url;

标签: php jquery


【解决方案1】:

您的window.location = ... 应该在validMA 函数内

function validMA(){
    $("#fullFormMA").click(function(e){
        e.preventDefault();
        $("#errorMA").html('');
        var fullForm = $(this).serialize();
        $.ajax({
            url: "validMA.php",
            type: "post",
            data: fullForm,
            success: function(result){
                $("#errorMA").html(result).fadeIn();

                /* ====
                 * HERE YOU PLACE IT
                 * ====
                 */
                window.location = "http://184.154.174.162/~nsgpcom/memberAppPay.php";


            },
            error:function(){
               $("#errorMA").html('There was an error.  Please try again.').fadeIn();
            }
        });//end 
    });
    $("errorMA").hide();
}//end function

【讨论】:

  • 感谢您的回复。我在那里试过。发生的情况是,一旦我点击进入我尝试使用 validMA.php 验证多个字段的表单,我就会立即被重定向到我的付款页面。是的,重定向在那里有效。但是,在将用户转移到付款页面之前,如何完成所有验证?
【解决方案2】:

你有

validMA();
window.location = "http://184.154.174.162/~nsgpcom/memberAppPay.php";

但我 window.location 进入了 validMA();

success: function(result){
            $("#errorMA").html(result).fadeIn();
            window.location = "http://184.154.174.162/~nsgpcom/memberAppPay.php";
        },

在这里!

【讨论】:

    【解决方案3】:

    我在使用上述建议时遇到的问题是,在所有验证完成之前,用户被重定向到付款页面……实际上是在第一次提交表单时。我在我的 validMA.php 代码的底部添加了以下代码(在我的 if/else 循环的成功部分中)。

    if ($error){
        echo "You need to fill in all required (*) fields";
    } else {
        $sql2 = "INSERT INTO newMembers (username,firstName,lastName,address1,address2,city,state.....)";
        $result2 = mysql_query($sql2) or die(mysql_error());?>
        <script type="text/javascript">
            alert('You completed step 1');
            window.location.href = "http://www.paymentPage.php";
        </script>
    <?php
    }?> 
    

    我觉得它没有它应该的那么优雅,但它确实有效。谢谢大家的建议。

    【讨论】:

      【解决方案4】:

      使用

      window.location.href="your URL"
      

      而不是

      window.location
      

      【讨论】:

      • 最好in解释一下为什么您的解决方案有效。您可能会在您提供的链接中重复一些解释,但它是一个更好的解决方案
      • 以上有语法错误,所以如果我会解释你有语法错误以及你将如何纠正它,那将是勺子喂食。我不同意你的反对意见。如果答案错误,您可以投反对票。
      猜你喜欢
      • 2020-11-13
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2014-06-10
      相关资源
      最近更新 更多