php重定向页面未打开到新位置但在div中打开ajax响应文本

时间:2014-10-23 04:22:29

标签: javascript php html ajax

php重定向页面未打开到新位置但在div中打开ajax响应文本

PHP > 问题> PHP + AJAX + HTML表格> REDIRECT [ ?] = 新位置

  

(?)= PAge应该在新页面中打开,但在div #ajax

中打开

欲望:在不在div中的新页面中打开


HTML CODE:

  <form id="form1" name="form1" action="login_success.php" onSubmit="return Ajxfunction()">

  <input type="text" name="username" autocomplete="off" id="username">
  <div id="validate"></div>
 <input id="passowrd" type="password" autocomplete="off" name="password">
   <div id="validate2"></div>
  <input type="checkbox" name="checkbox" id="checkbox">
  Stay Signed in <span>Forgetget Paswoard</span>
  <br>
  <input class="submit" id="button" type="button" value="SignIn" onClick=" Ajxfunction()" >
  <div id="ajaxDiv"></div>
  </form>

AJAX代码

      function Ajxfunction(){

              var text= text2="";
              var boolean=false;
            var user = document.forms["form1"]["username"].value;
             var password = document.forms["form1"]["password"].value;


              if ( user==null  || user == "") {
                text = "Please Enter user name";

                 boolean=false;

            } else {
                text = "";
                boolean=true;

            }

             if ( password==null  || password == "") {
                text2 = "Please Enter Password";

                 boolean=false;

            } else {
                text2 = "";
                boolean=true;

            }

         document.getElementById("validate").innerHTML =text;
         document.getElementById("validate2").innerHTML =text2;

         if(boolean){
                ajaxFunction();




             }

             //return true;
        }


        function ajaxFunction(){
         var ajaxRequest;  // The variable that makes Ajax possible!

         try{
           // Opera 8.0+, Firefox, Safari
           ajaxRequest = new XMLHttpRequest();
         }catch (e){
           // Internet Explorer Browsers
           try{
              ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
           }catch (e) {
              try{
                 ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
              }catch (e){
                 // Something went wrong
                 alert("Your browser broke!");
                 return false;
              }
           }
         }
         // Create a function that will receive data 
         // sent from the server and will update
         // div section in the same page.
         ajaxRequest.onreadystatechange = function(){
           if(ajaxRequest.readyState == 4){
              var ajaxDisplay = document.getElementById('ajaxDiv');
              ajaxDisplay.innerHTML = ajaxRequest.responseText;
           }
     }

     var username1="";
    var password1="";
     username1=document.forms["form1"]["username"].value;
     password1=document.forms["form1"]["password"].value;
    var queryString = "?username=" + username1 ;
     queryString +=  "&password=" + password1;
    /* var queryString = "?username1=" + username1 ;
     queryString +=  "&password1="+password1; /*+ "&sex=" + sex*/
     ajaxRequest.open("GET", "Checklogin.php"+queryString, true);
     ajaxRequest.send(null); 
    }

PHP代码:Checklogin.php

   <?php 



 $link = mysqli_connect('localhost','root','root','fly') or die("Error " .mysqli_error($link)); 


 $user="";
 $pass="";
if(isset($_GET['username'])){$user = $_GET['username'];} 
if(isset($_GET['password'])){ $pass = $_GET['password'];} 



$user = mysqli_real_escape_string($link,$user);
$pass = mysqli_real_escape_string($link,$pass);


/* $result = mysqli_query($link,"SELECT * FROM `useraccount` where username='$username' and password='$password' ");
*/ 
$msg="";
 $result = mysqli_query($link,"SELECT * FROM `useraccount` where username='$user' && password='$pass' ");
 $count = mysqli_num_rows($result);
 try{

     /*while($row = mysqli_fetch_array($result)) {
  echo $row['username'] . " " . $row['password'];
  echo "<br>";
}*/
 if($count==1){
    // session_start();
    // $_SESSION["username"] = $user;
    // $_SESSION["password"] = $pass;

//  $_SESSION['login_user']=$result['useraccount']; //Storing user session value.
//  $_SESSION['login_user']="HAroon"; //Storing user session value.
    echo "Right Password";
header("Location:http://www.formget.com/app/");
 // header('Location: login_success.php');

}
else {
$msg ="Wrong Username or Pdasswor";

  mysqli_close($link);

}
}catch(Exception $e){
$msg=   " Wrong";

    }

    echo $msg;

?>

======

1 个答案:

答案 0 :(得分:1)

您无法使用Location标头重定向页面以获取ajax请求。您必须为ajax获取一些响应文本,并使用已解析的响应文本重定向用户。这是如何使用JS代码重定向浏览器

if (responseText == 'OK'){ document.location.href = 'http://example.com/';}