AJAX 总是返回到主页


AJAX always returning into Home Page

>有没有机构可以帮助我解决问题。我有 3 个文件index.php process.js redirect_process.php.好吧,index.php是我的主页,里面有一个表格。并且每次单击button submit时都会调用process.js以验证表单的数据,因此,如果输入数据有错误,只需不要返回 true 并显示一些错误消息,否则返回 true,然后重定向到另一个页面redirect_process.php。这是我的process.js代码:

$(document).ready(function(){
    $('#form_side').submit(function() { 
  var name_this = /^[a-zA-Z-' ]+$/;
  var return_process = false;
  if(name_this.test(document.getElementById("input_field_one").value) == false){
    alert('Error!');
    return_process = false;
  }else{
    $.ajax({ 
                type:"POST",
                url:"./r_p/redirect_process.php",//the link to call redirect process
                data: $("#my_form").serialize(),
                success: function(response){
                $("#my_form").trigger('reset');
    } });
                //No more validation here, so i want to go into another page now by returning into TRUE
                return_process = true; //This will be the cause now to go to another page.
  }
  return return_process;
 });
});

所以让我们假设现在没有错误输入的数据,我现在想使用 redirect_process.php 重定向到另一个页面。这是我的redirect_process.php代码:

...Some validation here
if (data is ok, and no error) {
    //Redirect into correct page
    header('Location: http://localhost/test/correct_page.php'); 
}else{
    //Redirect into wrong page
    header('Location: http://localhost/test/wrong_page.php');   
}

问题是,它总是返回到index.php它没有在上面的任何链接中重定向。请我帮帮我。谢谢!

可以肯定的是,一切都会发生在你的索引中.php因为你在 PHP 文件中调用 header,你把它与 ajax 一起使用,如果你直接去redirect_process.php,标题会起作用(并且始终在 header();之后使用 exit();

在 PHP 中执行此操作

...Some validation here
if (data is ok, and no error) {
    echo('OK');
}else{
    echo('Not OK');
}

然后在 js 中的提交事件中

$(document).ready(function(){
    $('#form_side').submit(function(e) { 
  e.preventDefault;                              // use e.preventDefault;
  var name_this = /^[a-zA-Z-' ]+$/;
  var return_process = false;
  if(name_this.test(document.getElementById("input_field_one").value) == false){
    alert('Error!');
    return_process = false;
  }else{
    $.ajax({ 
                type:"POST",
                url:"./r_p/redirect_process.php",//the link to call redirect process
                data: $("#my_form").serialize(),
                success: function(response){
                $("#my_form").trigger('reset');
                if(response == 'OK'){
                 window.location.href= "http://localhost/test/correct_page.php";
                 }else{
                 window.location.href= "http://localhost/test/wrong_page.php";
                 }
    } });
                //No more validation here, so i want to go into another page now by returning into TRUE
                return_process = true; //This will be the cause now to go to another page.
  }
  return return_process;
 });
});

这应该可以做到:

$(document).ready(function(){
    $('#form_side').submit(function(event) { 
  var name_this = /^[a-zA-Z-' ]+$/;
  var return_process = false;
  if(name_this.test(document.getElementById("input_field_one").value) == false){
    alert('Error!');
    return_process = false;
  }else{
    $.ajax({ 
                type:"POST",
                url:"./r_p/redirect_process.php",//the link to call redirect process
                data: $("#my_form").serialize(),
                success: function(response){
                $("#my_form").trigger('reset');
    } });
  } 
      event.preventDefault(); //this line prevents the form from executing it's action, which is none and that triggers a reload.
  return false;
 });
});

您正在通过 jQuery.submit 调用 onsubmit 事件。解析此事件时,将触发表单操作。由于没有定义任何内容,因此您使用 AJAX,因此会触发重新加载。

event.preventDefault()

这可以防止事件触发操作。

 ...Some validation here
 if (data is ok, and no error) {
     //Redirect into correct page
     header('Location: http://localhost/test/correct_page.php'); 
    }else{
     //Redirect into wrong page
      header('Location: http://localhost/test/wrong_page.php');   
  }

标题行有效,但不适用于您的情况。它告诉浏览器加载页面响应。此响应加载到 AJAX 响应中,而不是重定向当前页面。使用@Mohamed-Yousef的答案作为指导。