Jump to content

Custom exceptions not working?


afish674
 Share

Recommended Posts

I'm trying to create some custom exceptions but I'm not having any luck, the code is just proceeding as if nothing is wrong when I leave empty fields. Also, I'd like to have the errors display within the form itself by creating some divs, rather than just showing a message at the end. How would I do this? Here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Birthday Reminders</title><link href="css/main-styles.css" rel="stylesheet" type="text/css" /><link href="css/redmond/jquery-ui-1.8.23.custom.css" rel="stylesheet" type="text/css" /></head><body><?phpif (!isset($_POST['submit'])){?><form name="addBirthday" method="post" action="submitted.php"><h2>Enter a birthday to add to your reminders:</h2><label for="fName">First Name:</label><input name="fName" id="FName" type="text" size="20" />    <label for="sName">Second Name:</label><input name="sName" id="SName" type="text" size="20" />    <label for="date">Birthday:</label>    <input name="date" id="date"/>    <label for="note">Note:</label>    <textarea name="note" id="note"></textarea>    <input name="submit" id="submit"  class="UIbutton" type="submit" value="Add Birthday" /></form><?php} else {class EmptyField extends Exception{ }$fName = $_POST['fName'];$sName = $_POST['sName'];$date = $_POST['date'];$note = $_POST['note'];try{  if (empty($fName)){   throw new EmptyField("First name!");  }  if(empty($sName)){   throw new EmptyField("Second name!");  }  if (empty($date)){   throw new EmptyField("Birthday!");  }}catch (EmptyField $e){  echo"<div class=\"ui-state-error\"><p>Please enter a ". $e->getMessage()."</p></div>";  exit();}echo "<h2>This birthday has been added to your birthdays!</h2><p>$fName $sName's birthday on $date has been added to your reminders.</p><a href=\"birthdays.php\" class=\"UIbutton\">Add Another?</a><a href=\"upcoming.php\" class=\"UIbutton\">Upcoming Birthdays</a><a href=\"bymonth.php\" class=\"UIbutton\">Overview by Month</a>";}?><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript" src="js/jquery-ui-1.8.23.custom.min.js"></script><script type="text/javascript" src="js/UIscripts.js"></script></body></html>

Link to comment
Share on other sites

I'm trying to create exceptions that trigger when specific fields are left blank (fName, sName & date). According to the book i'm following, something like the following should achieve this:

class EmptyField extends Exception{ }$fName = $_POST['fName'];$sName = $_POST['sName'];$date = $_POST['date'];$note = $_POST['note'];try{  if (empty($fName)){   throw new EmptyField("First name!");  }  if(empty($sName)){   throw new EmptyField("Second name!");  }  if (empty($date)){   throw new EmptyField("Birthday!");  }}catch (EmptyField $e){  echo"<div class=\"ui-state-error\"><p>Please enter a ". $e->getMessage()."</p></div>";  exit();

I want it so that when the form is submitted, if any of these fields are left blank, it throws the exception and creates an error div underneath the field in question. Currently using the above code (my first post), when the form is submitted it just proceeds to the echo statement at the bottom, if you don't fill in the fields it just uses blanks in the variables. If you do fill in the fields, it works as intended.

Edited by afish674
Link to comment
Share on other sites

  • 1 month later...

I found out the problem with this eventually. The form re-directs to a different page for processing (form action="submitted.php") before it got to the code defining the exception. Therefore it was bypassing it. After I changed the form to process using the same page the exceptions worked fine! Thanks for your help in troubleshooting.

Edited by afish674
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...