1,105,644 Community Members

PHP Mail Form Validation - Help Please?

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am a noob, I know ;) Anywho, I am trying to create a form validation and when it is successful, it says "Thank you. Success" or whatever. It's a basic form on a page that has an action of "POST". Here is the code on the processing FORM. Also, in the "If empty" segments, is there a code I can use for the Go Back button instead of putting in a hyperlink?

<?
If (empty($_POST['first_name']))
{
$errors[] = '<div class="goback">Please enter a name.<br><br><a href="http://www.xxxxxxxx.com/contact">Go Back</a></div>';
}
if (empty($_POST['email']))
{
$errors[] = '<div class="goback">Please enter an email address.<br><br><a href="http://www.xxxxxxxxx.com/contact">Go Back</a></div>';
}
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email']))
{
$errors[] = '<div class="goback">Please enter a valid email address.<br><br><a href="http://www.xxxxxxxxx.com/contact">Go Back</a></div>';
}
if (count($errors) == 0) 
{
// Process form
}

else
{
echo $errors[0]; 
}

?><?

mail("XXXXX@XXXXX.com", "Jonathan Website Form", "
First Name: $first_name
Last Name: $last_name
Email: $email
Phone: $phone
Interested In: $interest
I Want To Tell You: $comment,");

?>
Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

hey if u want to validate email very very strictlly then read

http://www.daniweb.com/forums/thread141944.html

Member Avatar
bennymartinson
Newbie Poster
11 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Probably the simplest solution to eliminating the back buttons is to make your form submit to its own page. Then, on the page that contains the form, you can nest your code above in a conditional like the following:

if ($_POST)
{
   // insert code to generate errors
   // send mail if no errors
}
if (!$_POST || $errors)
{
   if ($errors)
      {echo $errors[0]}

   //insert your form here
}

You could also have your form processing script redirect the user to to the form via include('yourform.php'); and send the error variable to it via GET or even a session if you have to.

Hope this helps.

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey, what I have so far works... as far as email validation, name validation, etc. The only probably I am having is when the form SUCCESSFULLY passes all the requirements, it shows a blank page. I want it to show a message. I got this far so I don't want to scrap my other code. I just didn't know if there was a IF statement i could do, which i think it may already contain. any help is appreciated.

Member Avatar
bennymartinson
Newbie Poster
11 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I may be misunderstanding your question, but couldn't you just use echo to write your message to the page in the area that you have commented "Process form"?

Also, i'm confused as to why the mail statement is not in this area as well. The way you have it now will send out an email even if the user's form didn't validate.

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Haha. Thanks Benny. Apparently I am a bigger NOOB than I thought. Ok back to square one. I need a form that will validate everything and obviously say, "Please enter a name" if no name is entered. You can see my form here -- jonathancrowe.com/contact

Test out the form by not entering a name.... I like transfers you to the page and says to go back. Any help is much appreciated. Thanks for your help Benny and everybody.

Member Avatar
nikesh.yadav
Posting Whiz in Training
220 posts since Feb 2008
Reputation Points: 4 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

is problem solved

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

No problem is not solved. I need to start from square one. If anybody has a solution to my problem I am all ears. Here is a recap on what I am looking for.

  • Form fields required for "first_name" and "email"

  • Email validation

  • If there are errors for example: first name wasn't entered, or email is invalid - I want it to say "Please enter name.(or email is invalid, depending on the error) Go Back"

  • If the form was successful and requirements were met, I want it to say, "Successfully sent."

If anybody could help me it would be a big help. You can see my form at jonathancrowe.com/contact

Member Avatar
bennymartinson
Newbie Poster
11 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The script you posted above should work for this if you add echo '<b>Sent Successfully!</b>'; where you have //process form written. Everything else that you are asking for it seems to already do fine.

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The script you posted above should work for this if you add echo '<b>Sent Successfully!</b>'; where you have //process form written. Everything else that you are asking for it seems to already do fine.

Am I suppose to get rid of // process form and replace it with what you said?

The thing is... somebody made a very valid point. The form is still being sent, even if they don't fill in their name and address.

Member Avatar
bennymartinson
Newbie Poster
11 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, you can replace //process form. Two slashes before something means that it is just a comment on the code. So getting rid of //process form won't change the script. In fact, this indicates that you should be putting the code that processes the form into this area. In this case, that would be the mail function.

So here's a solution:

<?php
If (empty($_POST['first_name']))
{
$errors[] = '<div class="goback">Please enter a name.<br><br><a href="http://www.xxxxxxxx.com/contact">Go Back</a></div>';
}
if (empty($_POST['email']))
{
$errors[] = '<div class="goback">Please enter an email address.<br><br><a href="http://www.xxxxxxxxx.com/contact">Go Back</a></div>';
}
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['email']))
{
$errors[] = '<div class="goback">Please enter a valid email address.<br><br><a href="http://www.xxxxxxxxx.com/contact">Go Back</a></div>';
}
if (count($errors) == 0) 
{
   if (mail("XXXXX@XXXXX.com", "Jonathan Website Form", "
   First Name: $first_name
   Last Name: $last_name
   Email: $email
   Phone: $phone
   Interested In: $interest
   I Want To Tell You: $comment,"))
      { echo '<b>Sent Successfully!</b>'; }
   else
      { echo '<div class="goback">An unexpected error occurred and the message was not sent.  Please try again.<br /><br /><a href="http://www.xxxxxxxxx.com/contact">Go Back</a></div>'; }

}
else
{
echo $errors[0]; 
}

?>

What I did was I moved the mail function into the if (count($errors) == 0) conditional where it belongs. But I nested it in a conditional itself, so if the mail for some reason isn't sent, the user will be told that an unexpected error occurred and they should try again. If the mail is sent, I have the script writing that the message is sent successfully.

Hope this helps.

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey Benny, I will be try your code later tonight. Thank you so much for your help. It makes sense, now I just got to put it in motion. Thanks again!

You taught me a lot.

I will keep you posted and let you know how it went.

Member Avatar
mrjoncrowe
Newbie Poster
10 posts since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Benny you are a life saver! Everything works fine. I tested it and we are good to go. Thanks for all your help.

My next goal is to try to implement a search feature on my site. Could you point me in the direction of a good tutorial?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article