Hi Guys,

I'm making a login form. I want to add validation to it but don't know how to do it. I have just added the echo statement as an error message. I want the message to appear on the index page.Can anyone help?

My php login form process.php

<?php 
		session_start();
		require_once('_include/ssi/dbconfig2.php');
		
			
				$admin = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
				if(!$admin) {
					die('Failed to connect to server: ' . mysql_error());
				}
				
				$db = mysql_select_db(DB_DATABASE);
				if(!$db) {
					die("Unable to select database");
				}
		
				$admin_username=$_POST['admin_username']; 
				$admin_pass=$_POST['admin_pass']; 
				
				$admin_username = stripslashes($admin_username);
				$admin_pass = stripslashes($admin_pass);
				$admin_username = mysql_real_escape_string($admin_username);
				$admin_pass = mysql_real_escape_string($admin_pass);
				
				$sql="SELECT * FROM admin WHERE admin_username='$admin_username' and admin_pass='$admin_pass'";
				$result=mysql_query($sql);
				
				
				$count=mysql_num_rows($result);
				$db = mysql_fetch_array($result);
				
				
			if($count==1){
				
				$_SESSION['admin_id'] = $db['admin_Id'];
				header("location:dashboard.php");
			}else{
				echo "Wrong Username or Password!";
			}
				
			ob_end_flush();
		?>

My login HTML Form Codes:

<?php include("_include/ssi/header2.php"); ?>
  <!--LOGIN FORM STARSTS HERE-->
 --------------------I WANT THE ERROR MESSAGE HERE-------------- 
  <form action="logincheck.php" method="post" name="adminlogin" id="adminlogin">
    <table width="360" class="c3" border="0" align="center">
      <tbody>
        <tr>
          <td width="84" height="50" valign="top"><p class="c3">
              <label>Username:</label>
            </p></td>
          <td colspan="2" valign="top"><input name="admin_username" type="text" class="login" size="25" /></td>
        </tr>
        <tr>
          <td height="62" valign="top"><p class="c3">
              <label>Password:</label>
            </p></td>
          <td colspan="2" valign="top"><input name="admin_pass" type="password" class="login" size="25" /></td>
        </tr>
        <tr>
          <td height="90" valign="top"></td>
          <td width="106" valign="top"><input name="Submit" type="submit" value="Login" class="loginb" /></td>
          <td width="202" valign="top"><input name="reset" type="reset" value="Reset" class="loginb" /></td>
        </tr>
      </tbody>
    </table>
  </form>
  <!--LOGIN FORM ENDS HERE-->
  <?php include("_include/ssi/footer.php"); ?>
Member Avatar for Zagga

Hi,

As you are already using sessions, you can just store the error message as a $_SESSION variable, then redirect the user to whatever page you want to display the error message on. You can then echo the $_SESSION variable wherever you want it with something like this . . .

if (isset($_SESSION['error_message'])){
   echo $_SESSION['error_message'];
}

Zagga

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.