jarvis Posted December 10, 2009 Share Posted December 10, 2009 Hi all, Hope someone can point out the obvious. I've a log in script, if you dont enter a username or pw, you get a red asterix show by the field and a pop up. If you enter an email but not the pw, it says Undefined variable: email_error . This is the code: // Validate the email address. if (!empty($_POST['email'])) { $e = escape_data($_POST['email']); } else { echo '<script type="text/javascript">alert("You forgot to enter your email address!");</script>'; #echo '<p class="error">You forgot to enter your email address!</p>'; $e = FALSE; $email_error = '<span class="required"><img src="images/star.gif" /></span>'; } // Validate the password. if (!empty($_POST['pass'])) { $p = escape_data($_POST['pass']); } else { $p = FALSE; echo '<script type="text/javascript">alert("You forgot to enter your password!");</script>'; $pw_error = '<span class="required"><img src="images/star.gif" /></span>'; #echo '<p class="error">You forgot to enter your password!</p>'; } My form looks like: <form action="login.php" method="post"> <table border="0" width="310"> <tr> <td><img src="images/email_address.gif" /></td> <td><div class="myBoxLh"></div><input type="text" name="email" style="width:150px;" maxlength="90" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /><div class="myBoxRh"></div><?php if (isset($_POST['submitted'])) { echo $email_error; }?></td> </tr> <tr> <td><img src="images/password.gif" /></td> <td><div class="myBoxLh"></div><input type="password" name="pass" style="width:150px;" maxlength="20" /><div class="myBoxRh"></div><?php if (isset($_POST['submitted'])) { echo $pw_error; }?></td> </tr> <tr> <td colspan="2"><div align="left"><?php if (isset($_POST['submitted'])) { echo $error_message; }?></div><div align="right"><input type="image" src="images/submit.jpg" name="submit" value="Login" class="submit_small" /></div> <input type="hidden" name="submitted" value="TRUE" /></td> </tr> </table> </form> How can I stop this error as I thought I'd coded the correct way? Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/184636-isset-undefined-variable/ Share on other sites More sharing options...
deansatch Posted December 10, 2009 Share Posted December 10, 2009 Define the variable as '' at the start of your script. e.g. $email_error = ''; Quote Link to comment https://forums.phpfreaks.com/topic/184636-isset-undefined-variable/#findComment-974712 Share on other sites More sharing options...
cags Posted December 10, 2009 Share Posted December 10, 2009 There are two places I can see at a glance that could throw an Undefined variable Notice. <td><div class="myBoxLh"></div><input type="password" name="pass" style="width:150px;" maxlength="20" /><div class="myBoxRh"></div><?php if (isset($_POST['submitted'])) { echo $pw_error; }?></td> ...and... <td><div class="myBoxLh"></div><input type="text" name="email" style="width:150px;" maxlength="90" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /><div class="myBoxRh"></div><?php if (isset($_POST['submitted'])) { echo $email_error; }?></td> The problem is in both cases right at the end, you have echo $email_error and echo $pw_error, since these values are only set if an error exists for that particular field you will get problems on the field that isn't at fault. You can fix it as suggested by deansatch and simply give the variables default values or you can use the isset function to check it has a value... <?php if (isset($_POST['submitted']) && isset($email_error)) { echo $email_error; }?> Infact in many ways you can probably remove the isset($_POST['submitted']) part because you are not using that field so it bares no significance. Edit: At a quick glance there is potentially a third place for $error_message. Quote Link to comment https://forums.phpfreaks.com/topic/184636-isset-undefined-variable/#findComment-974713 Share on other sites More sharing options...
jarvis Posted December 10, 2009 Author Share Posted December 10, 2009 Ah, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/184636-isset-undefined-variable/#findComment-974714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.