1

I'm new to Jquery, and I'm trying to create a login page, using JQuery and PHP but it doesn't respond when I click the login button, what I want is that after the login is success it redirect to index.html.

here is the JQuery code :

<script type="text/javascript" > 
$(document).ready(function() {
  $("form#memberlogin").submit(function(e) {
      dataString = $(this).serialize();
      $.post("check_login.php", dataString, function(data) {
          $('form#memberlogin').hide();
          window.location="index.html"
      });
      e.preventDefault(); 
  });
});
</script>

Here is my form code :

<form id="memberlogin" class="form-horizontal"  method="post">
                    <fieldset>
                        <div class="input-prepend" title="Username" data-rel="tooltip">
                            <span class="add-on"><i class="icon-user"></i></span><input id="username" autofocus class="input-large span10" name="username" id="username" type="text" value="admin" />
                        </div>
                        <div class="clearfix"></div>

                        <div class="input-prepend" title="Password" data-rel="tooltip">
                            <span class="add-on"><i class="icon-lock"></i></span><input id="password" class="input-large span10" name="password" id="password" type="password" value="admin123456" />
                        </div>
                        <div class="clearfix"></div>

                        <div class="input-prepend">
                        <label class="remember" for="remember"><input type="checkbox" id="remember" />Remember me</label>
                        </div>
                        <div class="clearfix"></div>

                        <p class="center span5">
                        <button type="submit" class="btn btn-primary">Login</button>
                        </p>
                    </fieldset>
                </form>

And this is my php code :

 <?php
 ob_start();
 $con=mysql_connect(localhost,"root","-----");
 if(!con)
 {
  die(mysql_error());
  exit();
 }

 mysql_select_db("sure") or die(mysql_error());

  $user=$_POST['username'];
  $pass=$_POST['password'];

if(get_magic_quotes_gpc()){
  $user = stripslashes($user);  //mencegah mysql injection
  $pass = stripslashes($pass);
}
  $user = mysql_real_escape_string($user);
  $pass = mysql_real_escape_string($pass);

  $result=mysql_query("SELECT * FROM user WHERE username='$user' and password='$pass'") or  die(mysql_error());
  $count=mysql_num_rows($result);
  if($count==1)
  {
    session_register("user");
    session_register("pass");
    $_SESSION['username']=$_POST['user'];
    $_SESSION['id']=mysql_result($result,0,'id');

    echo 'correct';
    header("location:index.html");
  }
  else
  {
    die("id atau password anda salah");
    exit();
  }

  mysql_close($con);
   ob_end_flush();
 ?>
stephen1706
  • 1,010
  • 11
  • 22

2 Answers2

0

try with this code.

if($count==1)
{
session_register("user");
session_register("pass");
$_SESSION['username']=$_POST['user'];
$_SESSION['id']=mysql_result($result,0,'id');

echo 'Y';
//header("location:index.html");
}
else
{
die("id atau password anda salah");
exit();
}

in javascript:

$.post("check_login.php", dataString, function(data) {
      // data is the response sent from the check_login.php

      // display the type of data
      console.log( typeof data );
      console.log( data );

      // if data is Y
      var reg = /Y/g;

      // regex to check the result
      if(  reg.test(data) ){

         console.log('IN');
         $('form#memberlogin').hide();

         //top.location.href = "index.html";

      } else {
         console.log('OUT');
         // PHP dies message here.
         //alert(data);
      }

      return false;
  });

try to check the response in firebug console tab.

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
0

Try to use href like,

window.location.href="index.html"

Read this What's the difference between window.location= and window.location.replace()?

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106