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,");

?>

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.

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.

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.

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.

is problem solved

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

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.

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.

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.

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.