Page 1 of 1

Mail form - the best way?

Posted: Thu Oct 10, 2002 12:52 am
by Lukenet
Hi all,

I am in the middle of creating a mail form that captures information from
a user > validates it (if ok) mails the info and goes to a thankyou page.

Now I have planed to create the following pages,

‘Main.php’ [the page where the blank original form is located on]

This submits to ‘send_feedback.php’ for validation and mailing..

Now, this is where I am stuck on how to handle this..

The functionality I want is;
If the form is incomplete or does not validate correctly, I want to take the user to a page that has the same structure as ‘main.php’, say it is called ‘error_feedback.php’ but it retains all the inputted text and selected options the user input on the ‘main.php’ page.

I then want to but an red star ‘*’ next to the field in error and display an error message containing a list of the errors contained within the form and an explanation of how the user should remedy the errors and resubmit the page again.. (maybe back through send_feedback.php) for revalidation and to sent the info via mail..

Now, this is what I am confused about, should I,

Which is the best way to do this???,
1. Storing all the values in a session variables (there currently aren’t any other session running on the site).
2. Pass all the values into a DB (mysql and retrieve them when I need them to be populated back into the error filled form)
3. Use an array to the same thing as the above
4. There is an option I have not considered but you know about that would be perfect for this type of thing..

Simpler the solution, the better for me..

Let me know what you think I should do, (in detail, code examples would be great, hint), and why you would use the method you think is best..

Any help on this would be very much appreciated..
Maybe if this thread gets a lot of good answers, it could be referred to when others have the same problem….

Thanks in advance..

Lukenet

Posted: Thu Oct 10, 2002 2:22 am
by sfhc
This is how i handle forms:

First I use OOP, so what i do may look a little odd, but if you grasp it, it will save you countless hours of development time and make your code reusable.. blah blah

Code: Select all

class MailForm{

/**
* this function builds the display of the form
* In mailForm.php, in the attributes of your form elements, you must
* do this:  name=subject value=<?php echo $postїsubject]; ?>
* subject is just for example, it could be to, from, messageBody.....
* @param array $post the array from $_POST
* @access public
*/
function buildForm($post){
  include "forms/mailForm.php";
}

/**
* this function validates the e-mail
* if the submission is valid, it sends the email and calls the function for 
* displaying the thank you pages.  If it is not valid, it out puts an error
* message and calls the function for the mail form, and passes $post 
* to it, so that the form can be pre-populated with data from previous
* tries.
* @param array $post array from $_POST
* @access private
*/
function validateMail($post){
   // do some validations
   if(empty($postїsubject])){
     echo "You left the subject field blank<br />";
     $this->buildForm($post);
   } elseif(!empty($postїsubject])){
       mail();
       $this->thankYouPage();
   }
}

/**
* This function builds the thank you page
* @access private
*/
function thankYouPage(){
  include "pages/thankYouPage.php";
}

}
That should get you going in the right direction.