javascript - AJAX PHP LOGIN Page is not redirecting correctly -


basically i've got ajax login page working , when login want redirect page without reloading i'm not sure how done new langauge. many thanks

index.php    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery.js"></script> <link rel="stylesheet" href="styles.css" type="text/css" /> <title>popup login</title> <script type="text/javascript"> $(document).ready(function(){     $("#login_a").click(function(){         $("#shadow").fadein("normal");          $("#login_form").fadein("normal");          $("#user_name").focus();     });     $("#cancel_hide").click(function(){         $("#login_form").fadeout("normal");         $("#shadow").fadeout();    });    $("#login").click(function(){          username=$("#user_name").val();         password=$("#password").val();          $.ajax({             type: "post",             url: "login.php",             data: "name="+username+"&pwd="+password,             success: function(html){               if(html=='1')               {                 $("#login_form").fadeout("normal");                 $("#shadow").fadeout();                 $("#profile").html("<a href='logout.php' id='logout'>logout</a>");                }               else               {                     $("#add_err").html("wrong username or password");               }             },             beforesend:function()             {                  $("#add_err").html("loading...")             }         });          return false;     }); }); </script> </head> <body> <?php session_start(); ?>     <div id="profile">         <?php if(isset($_session['user_name'])){             ?>             <a href='logout.php' id='logout'>logout</a>         <?php }else {?>         <a id="login_a" href="#">login</a>         <?php } ?>     </div>     <div id="login_form">         <div class="err" id="add_err"></div>         <form action="login.php">             <label>user name:</label>             <input type="text" id="user_name" name="user_name" />             <label>password:</label>             <input type="password" id="password" name="password" />             <label></label><br/>             <input type="submit" id="login" value="login" />             <input type="button" id="cancel_hide" value="cancel" />         </form>     </div>     <div id="shadow" class="popup"></div> </body> </html> 

login.php

    <?php  session_start();  $username = $_post['name'];  $password = $_post['pwd'];  $mysqli=mysqli_connect('');  $query = "select * user username='$username' , password='$password'";  $result = mysqli_query($mysqli,$query)or die(mysqli_error());  $num_row = mysqli_num_rows($result);  $row=mysqli_fetch_array($result);  if( $num_row >=1 ) {   echo '1';   $_session['user_name']=$row['username'];  }  else{  echo 'false';  } ?> 

when login want redirect page without reloading

as long page redirected on same domain, or can set cors headers properly, , can deal relative links, in success callback can replace entire page's html of new page.

you can start ajax call new page, , use document.write() obliterate existing html so:

... success: function(html){     if(html=='1') {         $("#login_form").fadeout("normal");         $("#shadow").fadeout(400, function(){              // replace page's html             $.ajax({                 type: 'get',                 url: 'http://example.com/logged-in',                 async: false, // want synchronous                 success: function(data) {                    // code block replaces current page seamlessly                   document.open();                   document.write(data);                   document.close();                  },                 error: function(e){                    alert(e.message);                 }             });         });     }     ... 

here demo:

$("#bye").fadeout(1400, function(){  	$.ajax({  		type: 'get',  		url: 'http://enable-cors.org/',  		async: false,  		success: function(data) {                    // quick-n-dirty hack                     // relative links working demo  		  var base_href = '<base href="http://enable-cors.org/">';    		  document.open();  		  document.write(base_href + data);  		  document.close();  		},  		error: function(e){  		   alert(e.message);  		}  	});  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <div id="bye"><h1>logged in successfully</h1></div>


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -