1

in the index Page, the user needs to login.. after login,

<?php
    include("dbinit.php");
    $text="";
    $eadd = $_POST['eadd'];
    $pass = $_POST['pass'];         
     if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
         $result = mysqli_query($link,"SELECT * FROM account WHERE Eadd='".$eadd."'");
        if (mysqli_num_rows($result)<=0){
          $text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
        }
        else
        {
         while($row = mysqli_fetch_array($result)){
        $passH = $row['Pass'];
            $passS = $row['hash'];
         }
     if(md5($pass.$passS) == $passH){
            $_SESSION['account'] = $eadd;
            $text = "<font color=red>Login Successful!</font>";
         }else{
            $text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
         }
            }
        mysqli_free_result($result);
    } else {
      $text = "<font color=red>Invalid Emailaddress!</font>";
    }
                mysqli_close($link);
echo $text;
?>

in the index Page,

function login(){
var eadd = $('#eadd').val();
var pass = $('#pass').val();
$.ajax({
type: "POST",
url: "login.php",
data: {eadd:eadd, pass:pass}
}).done(function( result ) {
$("#loginMsg").html(result);
});
}

how can i redirect or refresh the Page after he logged in? after loggedin, the index page must refresh.. do i need to Put window.history.pushState("", "", '/newpage');? how to use it?

John Bobs
  • 489
  • 1
  • 6
  • 12
  • 2
    **Danger**: You are vulnerable to [SQL injection](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php). – Quentin Jul 12 '13 at 08:23
  • i dont have any knowledge about sql injection.. canutell me whyi am vulnerable in that? – John Bobs Jul 12 '13 at 08:26
  • There is a link in my previous comment. Read *the very first sentence* after the main heading. – Quentin Jul 12 '13 at 08:27

2 Answers2

0
window.top.location.reload();

Use that in your ajax success callback

To redirect instead to a differnt page use:

window.top.location = '/some/page/withcats';

Use:

function login(){
  var eadd = $('#eadd').val();
  var pass = $('#pass').val();
  $.ajax({
    type: "POST",
    url: "login.php",
   data: {eadd:eadd, pass:pass}
  }).done(function( result ) {
    $("#loginMsg").html(result);
    //you may want to check result has no errors or something
    window.top.location.reload();
  });
}

Error handling:

You might want to check for an error, so that if the login is unsuccessful you do not want to refresh the page. To do that relies on knowing what you php function will return E.g.:

function login(){
      var eadd = $('#eadd').val();
      var pass = $('#pass').val();
      $.ajax({
        type: "POST",
        url: "login.php",
       data: {eadd:eadd, pass:pass}
      }).done(function( result ) {
        //this will make sure the page only refreshes if login is succesful, if not display error
        if(result === "<font color=red>Login Successful!</font>"){
          window.top.location.reload();
        }else{
          $("#loginMsg").html(result);
        }
      });
    }
1321941
  • 2,139
  • 5
  • 26
  • 49
  • how do i use it to my succcess callback? – John Bobs Jul 12 '13 at 08:30
  • function login(){ var eadd = $('#eadd').val(); var pass = $('#pass').val(); $.ajax({ type: "POST", url: "login.php", data: {eadd:eadd, pass:pass}, success: function(response){ if(response.success == true){ window.top.location.reload(); } } }).done(function( result ) { $("#loginMsg").html(result); }); } – John Bobs Jul 12 '13 at 08:32
  • in the code, how can i check if their is an error?so it will not reloadif has an error. – John Bobs Jul 12 '13 at 08:42
  • thanks again. i have slow internet connection so i cant see some edits :) – John Bobs Jul 12 '13 at 08:47
0

how can i redirect or refresh the Page after he logged in?

by using a regular form submission instead of Ajax.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335