Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP jQuery
hi friends
 

please help me
 
when give invalid details display popup and show alert
 
i.e if i given invalid details for login (don't match db table) then i want to dispaly popus as customized(my own popup) in that box display error measege
 
my code is here and give solution for this
 

 
<link rel="stylesheet" href="css/jquery-ui.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    window.$required = $('<div></div>').dialog({
        autoOpen: false,
        title: 'Required Information'
    });
});
 
</script>
 
<script type="text/javascript">
 
function validate_login ( )
{
    valid = true;
 
        if ( document.login.user.value == "" )
        {
               // alert ( "Please fill the user name." );

         $required.html("Please fill the user name.").dialog('open');
                valid = false;
        }
        else if ( document.login.pass.value == "" )
                {
                           $required.html("Please fill the password.").dialog('open');
                        valid = false;
        }
        return valid;
}
 

</script>
</head>
 

 

<td >
<form  name="login" method="post" action="login.php" onSubmit="return validate_login( );">
       <div class="webmail-bg">
         <table width="301" border="0" cellspacing="0" cellpadding="0">
           <tr>
             <td height="28" align="left" valign="top">Username :</td>
           </tr>
           <tr>
             <td><label>
               <input name="user" id="sPerson" type="text" class="email-txt-field" value="" />
             </label></td>
           </tr>
           <tr>
             <td height="38" align="left" valign="middle">Password :</td>
           </tr>
           <tr>
             <td><label>
               <input  type="password" name="pass" type="text" class="email-txt-field" value="" />
             </label></td>
           </tr>
 

 

---------->code for check for login details with DB table-------->
 
<?php
ob_start();
include "db.php";
 

$username=$_POST["user"];
$password=$_POST["pass"];
 
$username=strip_tags(stripslashes($username));
$password=strip_tags(stripslashes($password));
 
//now md5 the password

//$pass=md5($password);

$mysqlqry="select username,password from admin_login where username='".$username."' and password='".$password."'";
 

//echo $mysqlqry;
$result = mysql_query($mysqlqry);
$count=mysql_affected_rows();
//echo "Result is ".$result;
//echo $count;

 

 

if(!$result)
{
    echo("<P>Can't execute the query.</P>");
    echo("<P>Error performing query: " . mysql_error() . "</P>");
    include "index.php";
    exit();
}
 
    else
    if($count==1)
    {
            setcookie("login","User",time()+2*3600);
header('Location: adminhome.php');
        }
else        {
echo "<script> alert('invalid username or password')</script>";
//echo "Invalid Username or Password.<br>";
include "index.php";}?>
Posted 24-Feb-13 22:59pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may use jQuery ajax to check valid username/password so that you can use the dialog box to show the error messages.
 
Here I have modified function validate_login() and the login.php codes:
 
modified function validate_login() is:
function validate_login ( )
{
    //valid = true;

    if ( document.login.user.value == "" )
    {
        // alert ( "Please fill the user name." );
        $required.html("Please fill the user name.").dialog('open');
        //valid = false;
    }
    else if ( document.login.pass.value == "" )
    {
        $required.html("Please fill the password.").dialog('open');
        //valid = false;
    }
 
    else {
 
        $.ajax({
            type: "POST",
            url: "login.php",
            data: "user="+$("input[name='user']").val()+"&pass="+$("input[name='pass']").val(),
            success: function(msg){
 
                if(msg == 'success') {
                    window.location.href= "adminhome.php";
                }
                else {
                    $required.html(msg).dialog('open');
                }
            }
        });
    }
 
    return false;
}
 
updated login.php codes:
ob_start();
include "db.php";
 

$username=$_POST["user"];
$password=$_POST["pass"];
 
$username=strip_tags(stripslashes($username));
$password=strip_tags(stripslashes($password));
 
//now md5 the password

//$pass=md5($password);

$mysqlqry="select username,password from admin_login where username='".$username."' and password='".$password."'";
 

//echo $mysqlqry;
$result = mysql_query($mysqlqry);
$count=mysql_affected_rows();
//echo "Result is ".$result;
//echo $count;

 
if(!$result)
{
    echo("Can't execute the query.<br />");
    echo("Error performing query: " . mysql_error() . "");
    //include "index.php";
    exit();
}
else
    if($count==1)
    {
        setcookie("login","User",time()+2*3600);
        //header('Location: adminhome.php');
        echo("success");
        exit();
    }
    else
    {
        //echo "<script> alert('invalid username or password')</script>";
        echo "Invalid Username or Password.";
        //include "index.php";
        exit();
    }
?>
 
Finally, I have removed the action="login.php" from the form tag:
<form name="login" method="post" onsubmit="return validate_login( );"></form>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100