Page 1 of 1

Displaying error message on the same page !

Posted: Sun Jul 05, 2009 12:51 am
by aditi_19
I have a php file named change.php to change the existing password. This page takes username, current password and new password. change.php is directed to verify.php which has to check if the username and current password are correct or not and likewise update it with a new password.

I am unable to load the error message on the same page i.e. change.php and ask the user to try entering again if either the username or existing password are incorrect !

How can this be done? I want to load the error message on page change.php and ask user to enter again !

change.php :

<form name="form1" method="post" action="verify.php">

<input name="userid" type="text" size="9" maxlength="9">
<input name="oldpassword" type="password" size =20" value="" maxlength="20" autocomplete="off" >
<input name="newpassword" type="password" size="20" value="" maxlength="20" autocomplete="off">
<input type="submit" name="Submit" id="Submit" value="Submit">
</form>


verify.php :

<?php
session_start();

$username =$_POST['userid'];
$oldpwd = $_POST['oldpassword'];
$newpwd = $_POST['newpassword'];

$user = "root";
$host = "localhost";
$pswd = "project";
$dbname = "project" ;
mysql_connect($host,$user,$pswd);
mysql_select_db($dbname);

$query = "SELECT password from tbluserentry where username = '$username';";
$result= mysql_query($query);

$passwordhash = md5($oldpwd);

for ($i=0 ;$i<mysql_num_rows($result); $i++)
{
$row_array = mysql_fetch_row($result);
for($j=0 ;$j<mysql_num_fields($result); $j++)
{
if(($row_array[$j]) == $passwordhash)
{
$query = "update tbluserentry set password= md5('$newpwd') where username='$username';";
$result= mysql_query($query);
$_SESSION['global'] = 1;
header( 'Location: update.php' ) ;
}
else
header( 'Location: change.php' ) ;
}
}
?>

Please help me !

Re: Displaying error message on the same page !

Posted: Sun Jul 05, 2009 1:02 am
by requinix
You know, the easiest way to do this would be to combine change.php and verify.php into one file...

Code: Select all

<?php
 
// display your HTML up until the point where you display
// the error/success message
 
if (/* form was submitted */) {
    if (/* old password is correct */) {
        // change password
        // show success message
    } else {
        // show error message
    }
}
 
// display the form
 
// display the rest of your HTML
 
?>
That's the basic idea, but there are many ways to do this.

Re: Displaying error message on the same page !

Posted: Sun Jul 05, 2009 1:40 am
by paqman
For this kind of thing I make it one page as well. And you can probably set up your system so usernames have to be unique, and take out that loop in this script.

Code: Select all

<?
session_start();
 
if(isset($_POST["submit"]))
{
   $username =$_POST['userid'];
   $oldpwd = $_POST['oldpassword'];
   $newpwd = $_POST['newpassword'];
 
   $user = "root";
   $host = "localhost";
   $pswd = "project";
   $dbname = "project" ;
   mysql_connect($host,$user,$pswd);
   mysql_select_db($dbname);
 
   $query = "SELECT password from tbluserentry where username = '$username'"; //took out the semicolon
   $result= mysql_query($query);
 
   $passwordhash = md5($oldpwd);
 
   for ($i=0 ;$i<mysql_num_rows($result); $i++)
   {
      $row_array = mysql_fetch_row($result);
      for($j=0 ;$j<mysql_num_fields($result); $j++)
      {
         if(($row_array[$j]) == $passwordhash)
         {
            $query = "update tbluserentry set password= md5('$newpwd') where username='$username'";
            $result= mysql_query($query);
            $_SESSION['global'] = 1;
            ?><p>You're good</p><?
         }
 
         else
         {
            ?><p>Couldn't work it out</p><?
         }
      }
   }
}
 
else
{
?><form name="form1" method="post" action="verify.php">
 
<input name="userid" type="text" size="9" maxlength="9">
<input name="oldpassword" type="password" size =20" value="" maxlength="20" autocomplete="off" >
<input name="newpassword" type="password" size="20" value="" maxlength="20" autocomplete="off">
<input type="submit" name="Submit" id="Submit" value="Submit">
</form><?
}
?>
 

Re: Displaying error message on the same page !

Posted: Sun Jul 05, 2009 12:29 pm
by aditi_19
Hey,

I got it working the way you told. Thanks a lot for your help !

Aditi