du229908 2014-10-23 04:22
浏览 32
已采纳

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

php redirect page is not open to new location but open in the div where ajax response text

PHP>ISSUE>PHP + AJAX + HTML FORM>REDIRECT [?]=NEW LOCATION.

(?)=PAge should be open in new page but open in the div #ajax

desire:Open in new page not in the 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 CODE

      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 CODE: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条回答 默认 最新

  • dongpeng0127 2014-10-23 04:39
    关注

    You can not redirect page with Location header for ajax requests. You must get some response text for ajax and redirect user using parsed response text. This is how to redirect browser with JS code

    if (responseText == 'OK'){ document.location.href = 'http://example.com/';}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?