Displaying error message on the same page !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aditi_19
Forum Newbie
Posts: 23
Joined: Sun Jun 21, 2009 3:09 pm

Displaying error message on the same page !

Post 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 !
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Displaying error message on the same page !

Post 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.
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: Displaying error message on the same page !

Post 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><?
}
?>
 
aditi_19
Forum Newbie
Posts: 23
Joined: Sun Jun 21, 2009 3:09 pm

Re: Displaying error message on the same page !

Post by aditi_19 »

Hey,

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

Aditi
Post Reply