Click here to Skip to main content
16,012,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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



XML
<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>


XML
<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>





XML
<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
<?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

1 solution

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:
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;
    }

    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:
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("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>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900