Jump to content


Photo

Not sure why...


  • Please log in to reply
4 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 17 July 2006 - 12:32 PM

I'm writing a script so that members can change their passwords.
I'm starting the script by checking the form for errors. The first error i'm checking for is empty fields. This below script does this semi-successfully. It prints out that an error has occurred and what the errors are depending on which field is empty. The problem is that it always prints out that there has been an error, even if all the feilds have been filled in.

<?php
session_start();
include 'db.php';
include 'session_check.php';

$current_pass = $_POST['current_pass'];
$new_pass = $_POST['new_pass'];
$confirm_pass = $_POST['confirm_pass'];

//Checking for empty fields
if ((empty($current_pass)) || (empty($new_pass)) || (empty($confrim_pass))) {
echo '<div class="error">*The following errors occurred:</div>' . "\n";

if (empty($current_pass)) {
echo '<div class="error">You did not specify your Current Password</div>' . "\n";
}

if (empty($new_pass)) {
echo '<div class="error">You did not specify your New Password</div>' . "\n";
}

if (empty($confirm_pass)) {
echo '<div class="error">You did not Confirm your New Password</div>' . "\n";
}
include 'Account_Manager.php';
exit();
}
?>

Can any tell me why, and how to fix it?

Thanks in advance.

-Tandem

#2 brown2005

brown2005
  • Members
  • PipPipPip
  • Advanced Member
  • 943 posts

Posted 17 July 2006 - 12:36 PM

if((!$username) || (!$password) || (!$number))
{
     
if(!$username)
{
$error .= "<tr><td>You have not entered your username.</td></tr>"; 
}     
if(!$password)
{
$error .= "<tr><td>You have not entered your password.</td></tr>";
}
if(!$number)
{
$error .= "<tr><td>You have not entered the verification code.</td></tr>";
}   

}

try that mate, it is wat i use

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 July 2006 - 12:42 PM

the only problem with that code, brown2005, is that it will throw warnings whenever your form is not submitted yet. you can't refer directly to the variables without checking to see if they are set yet.

the best bet is to do something like this:
<?php
if (isset($_POST['submit'])) {
  foreach ($_POST as $key => $val) ${$key} = trim($val);
  if (empty($current_pass) || empty($new_pass) || empty($confirm_pass)) {
    echo "<div class=\"error\">The following error has occurred:</div>\n";
    if (empty($current_pass)) echo '<div class="error">You did not specify your Current Password</div>' . "\n";
    if (empty($new_pass)) echo '<div class="error">You did not specify your New password</div>' . "\n";
    if (empty($confirm_pass)) echo '<div class="error">You did not Confirm your New Password</div>' . "\n";
  }
}
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 17 July 2006 - 12:44 PM

you got a mis-spelling in var name:

empty($confrim_pass) from the if statement,
change to $confirm from $confrim
Life's too short for arguing.

#5 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 17 July 2006 - 12:49 PM

Ahhh, thankyou hvle.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users