how to show error message whenever the person enters wrong or empty text fields in th login form itself rather than linking to other error page? error message should be displayed just above the login text fields...
please pass me the codes so that i can understand.
thank you
vijukumar 0 Light Poster
please somebody reply
ryan_vietnow 13 Posting Pro
you can do it with javascript:
for example:
<script type="text/javascript">
function validate(form)
{
if (form.text1.value == "")
{
document.getElementById("txterror").innerHTML="missing fields";
return false;
}
}
</script>
<form name="form1" onsubmit="return validate(this);">
<p><span id="txterror"></span></p>
<input type="text" name="text1" />
<input type="submit" name="submit"/>
</form>
vijukumar 0 Light Poster
thanks a lot but here i should not use any java script ok i will send you the codes please help me.
index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoice System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><br>
<br>
<br>
<table width="36%" border="0" cellpadding="8" cellspacing="0" class="text">
<tr>
<td align="center" class="logo"><img src="graphics/cr8invoice.gif" width="137" height="38"><br></td>
</tr>
<tr>
<td align="center" bgcolor="#E2E2E2" class="text"> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table width="27%" border="0" cellpadding="6" cellspacing="0" bordercolor="#C9C9C9">
<tr>
<td width="24%" class="text">Name</td>
<td width="76%" colspan="2"><input name="admin_name" type="text" class="typeforms"></td>
</tr>
<tr>
<td class="text">Password</td>
<td> <input name="admin_password" type="text" class="typeforms"></td>
<td><input name="Submit" type="submit" class="typeforms" value="Submit"></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>
tables:
CREATE TABLE `administrator` (
`admin_id` tinyint(4) unsigned NOT NULL auto_increment,
`admin_name` varchar(255) NOT NULL,
`admin_password` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
loginck.php:
<?
$dbservertype='mysql';
$servername='localhost';
$dbusername='root';
$dbpassword='admin';
$dbname='invoice';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
<?php
$userid=mysql_real_escape_string($admin_name);
$password=mysql_real_escape_string($admin_password);
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM administrator WHERE admin_name='$admin_name' AND admin_password = '$admin_password'")))
{
if(($rec['admin_name']==$admin_name)&&($rec['admin_password']==$admin_password))
{
include "tt/include/newsession.php";
echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
print " self.location='admin_logged.php';"; // Comment this line if you don't want to redirect
print "</script>";
}
}
?>
please tell me how to show error message in the same index.php
rather than connecting to the other page
please send me the codes
Edited by Dani because: Formatting fixed
vijukumar 0 Light Poster
thank a ton ryan that java script helped me a lot..
will pass me some more test conditions as u mentioned here for empty fields.
if (form.admin_name.value == "")
{
document.getElementById("txterror").innerHTML="Please enter the valid admin name";
return false;
}
some more for comparing with data base?
forzadraco 0 Junior Poster in Training
Here the code:
ndex.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoice System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><br>
<br>
<br>
<table width="36%" border="0" cellpadding="8" cellspacing="0" class="text">
<tr>
<td align="center" class="logo"><img src="graphics/cr8invoice.gif" width="137" height="38"><br></td>
</tr>
<?php
if(!empty($msg) and $_REQUEST['msg']=="error"){
$msg="Sorry Your Login is not valid";
echo""
}
?>
<tr>
<td align="center" bgcolor="#E2E2E2" class="text"> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table width="27%" border="0" cellpadding="6" cellspacing="0" bordercolor="#C9C9C9">
<tr>
<td width="24%" class="text">Name</td>
<td width="76%" colspan="2"><input name="admin_name" type="text" class="typeforms"></td>
</tr>
<tr>
<td class="text">Password</td>
<td> <input name="admin_password" type="text" class="typeforms"></td>
<td><input name="Submit" type="submit" class="typeforms" value="Submit"></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>
tables:
CREATE TABLE `administrator` (
`admin_id` tinyint(4) unsigned NOT NULL auto_increment,
`admin_name` varchar(255) NOT NULL,
`admin_password` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
loginck.php:
<?
$dbservertype='mysql';
$servername='localhost';
$dbusername='root';
$dbpassword='admin';
$dbname='invoice';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
<?php
$userid=mysql_real_escape_string($admin_name);
$password=mysql_real_escape_string($admin_password);
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM administrator WHERE admin_name='$admin_name' AND admin_password = '$admin_password'")))
{
if(($rec['admin_name']==$admin_name)&&($rec['admin_password']==$admin_password))
{
include "tt/include/newsession.php";
echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
print " self.location='admin_logged.php';"; // Comment this line if you don't want to redirect
print "</script>";
}else{
header("location: index.php?msg=error");
}
}
?>
Edited by mike_2000_17 because: Fixed formatting
forzadraco 0 Junior Poster in Training
Sorry here the true code:
ndex.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoice System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><br>
<br>
<br>
<table width="36%" border="0" cellpadding="8" cellspacing="0" class="text">
<tr>
<td align="center" class="logo"><img src="graphics/cr8invoice.gif" width="137" height="38"><br></td>
</tr>
<?php
if(!empty($msg) and $_REQUEST['msg']=="error"){
$msg="Sorry Your Login is not valid";
echo"<tr><td colspan=2>$msg</td></tr>";
}
?>
<tr>
<td align="center" bgcolor="#E2E2E2" class="text"> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table width="27%" border="0" cellpadding="6" cellspacing="0" bordercolor="#C9C9C9">
<tr>
<td width="24%" class="text">Name</td>
<td width="76%" colspan="2"><input name="admin_name" type="text" class="typeforms"></td>
</tr>
<tr>
<td class="text">Password</td>
<td> <input name="admin_password" type="text" class="typeforms"></td>
<td><input name="Submit" type="submit" class="typeforms" value="Submit"></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>
tables:
CREATE TABLE `administrator` (
`admin_id` tinyint(4) unsigned NOT NULL auto_increment,
`admin_name` varchar(255) NOT NULL,
`admin_password` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
loginck.php:
<?
$dbservertype='mysql';
$servername='localhost';
$dbusername='root';
$dbpassword='admin';
$dbname='invoice';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
<?php
$userid=mysql_real_escape_string($admin_name);
$password=mysql_real_escape_string($admin_password);
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM administrator WHERE admin_name='$admin_name' AND admin_password = '$admin_password'")))
{
if(($rec['admin_name']==$admin_name)&&($rec['admin_password']==$admin_password))
{
include "tt/include/newsession.php";
echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
print " self.location='admin_logged.php';"; // Comment this line if you don't want to redirect
print "</script>";
}else{
header("location: index.php?msg=error");
}
}
?>
Edited by Dani because: Formatting fixed
ryan_vietnow 13 Posting Pro
here I made your validation part:Just insert the database insertion where I have placed the comment,ok?
<?php
//if the user clicks submit button...
if ($_POST['Submit'])
{
//check for missing fields
if ($_POST['admin_name']=="")
{
$name_missing="missing fields";
}
elseif($_POST['admin_password']=="")
{
$pass_missing="missing fields";
}
else
{
//do your insertion in database here...
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoice System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><br>
<br>
<br>
<table width="36%" border="0" cellpadding="8" cellspacing="0" class="text">
<tr>
<td align="center" class="logo"><img src="graphics/cr8invoice.gif" width="137" height="38"><br></td>
</tr>
<tr>
<td align="center" bgcolor="#E2E2E2" class="text"> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table width="27%" border="0" cellpadding="6" cellspacing="0" bordercolor="#C9C9C9">
<tr>
<td width="100%"><?php echo $name_missing;?></td>
</tr>
<tr>
<td width="24%" class="text">Name</td>
<td width="76%" colspan="2"><input name="admin_name" type="text" class="typeforms"></td>
</tr>
<tr bordercolor="#000000">
<td width="100%"><?php echo $pass_missing;?></td>
</tr>
<tr>
<td class="text">Password</td>
<td> <input name="admin_password" type="text" class="typeforms"></td>
<td><input name="Submit" type="submit" class="typeforms" value="Submit"></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>
vijukumar 0 Light Poster
ryan it is still not working
<form name="form1" method="post" action="<?php echo $_SERVER?>">
is it alrite?
#
else
#
{
#
//do your insertion in database here...
#
} here i am not inserting just comparing with the database please help me
vijukumar 0 Light Poster
ryan consider here is my index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoice System</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><br>
<br>
<br>
<table width="36%" border="0" cellpadding="8" cellspacing="0" class="text">
<tr>
<td align="center" class="logo"><img src="graphics/cr8invoice.gif" width="137" height="38"><br>
</td>
</tr>
<tr>
<td align="center" bgcolor="#E2E2E2" class="text"> <form name="form1" method="post" action="loginck.php">
<p><span id="txterror"></span></p>
<table width="27%" border="0" cellpadding="6" cellspacing="0" bordercolor="#C9C9C9">
<tr>
<td width="24%" class="text">Name</td>
<td width="76%" colspan="2"><input name="admin_name" type="text" class="typeforms"></td>
</tr>
<tr>
<td class="text">Password</td>
<td> <input name="admin_password" type="text" class="typeforms"></td>
<td><input name="Submit" type="submit" class="typeforms" value="Submit"></td>
</tr>
</table>
<br>
</td>
</tr>
</table>
</body>
</html>
tables:
CREATE TABLE `administrator` (
`admin_id` tinyint(4) unsigned NOT NULL auto_increment,
`admin_name` varchar(255) NOT NULL,
`admin_password` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
<?
$dbservertype='mysql';
$servername='localhost';
$dbusername='root';
$dbpassword='admin';
$dbname='invoice';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
$userid=mysql_real_escape_string($admin_name);
$password=mysql_real_escape_string($admin_password);
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM administrator WHERE admin_name='$admin_name' AND admin_password = '$admin_password'")))
{
if(($rec==$admin_name)&&($rec==$admin_password))
{
include "tt/include/newsession.php";
echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
print "<script>";
print " self.location='admin_logged.php';"; // Comment this line if you don't want to redirect
print "</script>";
}
}
?>
please i just want to display a validation error message just above the Name text box only using php please tell me how to do. with tha codes
thanks a lot
vijukumar 0 Light Poster
and mention the action of form fields well.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.