Jump to content

redirect to different pages accoring to login identification through ajax in php


rohan.bajaj17

Recommended Posts

i have table login master in database (username,password,status)...i differentiate logins according to status  (tm,oa,tl,frnc,dealer,me,admin)...when username,password and status matches it redirects to related header......so please give me solution ....solution must be through ajax and php.....here wht i did......see examle below......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=windows-1252" />

<title></title>

<link rel="stylesheet" type="text/css" href="style/style.css" />

<link rel="stylesheet" type="text/css" href="style/admin.css" />

 

<script src="js/jquery.js" type="text/javascript" language="javascript"></script>

<script language="javascript">

 

$(document).ready(function()

{

$("#login_form").submit(function()

{

//remove all the class add the messagebox classes and start fading

$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);

//check the username exists or not from ajax

$.post("ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)

        {

 

  if(data=='yes'&& id=='1') //if correct login detail

  {

  $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox

{

  //add message and change the class of the box and start fading

  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,

              function()

  {

  //redirect to secure page

document.location = '../oshonicaoriginalfinal/topmanagement/top_management_admin.php';

  });

 

 

});

 

 

  }

  else

  {

  $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox

{

  //add message and change the class of the box and start fading

  $(this).html('Your login detail is wrong.....').addClass('messageboxerror').fadeTo(900,1);

});

          }

 

        });

return false; //not to post the  form physically

});

//now call the ajax also focus move from

$("#password").blur(function()

{

$("#login_form").trigger('submit');

});

});

</script>

 

 

 

 

<style type="text/css">

<!--

.buttondiv {margin-top: 10px;

}

.top {margin-bottom: 15px;

}

-->

</style>

 

 

 

<style type="text/css">

body {

font-family:Verdana, Arial, Helvetica, sans-serif;

font-size:11px;

}

.top {

margin-bottom: 15px;

}

.buttondiv {

margin-top: 10px;

}

.messagebox{

position:absolute;

width:100px;

margin-left:30px;

border:1px solid #c93;

background:#ffc;

padding:3px;

}

.messageboxok{

position:absolute;

width:auto;

margin-left:30px;

border:1px solid #349534;

background:#C9FFCA;

padding:3px;

font-weight:bold;

color:#008000;

 

}

.messageboxerror{

position:absolute;

width:auto;

margin-left:30px;

border:1px solid #CC0000;

background:#F7CBCA;

padding:3px;

font-weight:bold;

color:#CC0000;

}

 

.buttondiv1 {margin-top: 10px;

}

.buttondiv1 {margin-top: 10px;

}

</style>

</head>

<body>

 

 

<div id="main_container">

 

<div id="header">

 

      <div id="logo"></div>

         

      <div class="banner_adds"></div>   

 

 

 

 

 

 

 

 

 

</div>

   

 

   

    <div id="main_content">

   

        <div class="admin_login">

       

       

           

                <div class="center_right_box">

               

        <form action="" method="post" name="login_form" id="login_form">

              <div align="center">

                <div class="top" ><span class="box_title"><span>Admin</span> login</span></div>

                <div > User Name :

                  <input name="username" type="text" id="username" value="" maxlength="20" />

                </div>

                <div style="margin-top:5px" > Password :

                    

          <input name="password" type="password" id="password" value="" maxlength="20" />

                </div>

                <div class="buttondiv">

    <input name="Submit" type="submit" id="submit" value="Login" style="margin-left:-10px; height:23px"  /> <span id="msgbox" style="display:none"></span>

 

              </div>

             

 

           

                  </form>

                  </div>

                  </div>

                   

       

           

   

<!-- end of main_content -->

   

 

 

 

 

</div>

 

<!-- end of main_container -->

 

</body>

</html>

 

 

 

 

ajax_login.php

 

<?php session_start();

 

 

//Connect to database from here

$link = mysql_connect('localhost', 'root', '');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

//select the database | Change the name of database from here

mysql_select_db('osho');

 

//get the posted values

$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES);

$pass=($_POST['password']);

 

//now validating the username and password

$sql="SELECT username, password,status FROM login_master WHERE username='".$user_name."'";

$result=mysql_query($sql);

$row=mysql_fetch_array($result);

$status[]='tm';

$status[]='oa';

 

//if username exists

if(mysql_num_rows($result)>0)

{

//compare the password

if(strcmp($row['password'],$pass)==0)

{

for($i=0;$i<count($status);$i++)

{

if(strcmp($row['status'],$status[$i])=='tm')

{

 

 

echo "yes";

 

header("loaction:../oshonicaoriginalfinal/topmanagement/top_management_admin.php?id=1");

$_SESSION['u_name']=$user_name;

 

}

if(strcmp($row['status'],$status[$i])=='oa')

{

 

 

echo "yes";

header("loaction:../oshonicaoriginalfinal/oa/oa_admin.php?id=2");

 

//now set the session from here if needed

$_SESSION['u_name']=$user_name;

 

}

}

 

 

}

 

else

echo "no";

}

else

echo "no"; //Invalid Login

 

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.