Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ò Form/Email Validation & Form Mailer

VALIDATION MAILERS

ò Form/Email Validation & Form Mailer

by  SPYDERIX  Posted    (Edited  )
This script is for form validation to determine if all fields have been filled out and if the email address supplied is of valid syntax. If all statements return true then the form is emailed to the specified recipient.

This script's main feature is that is replicates your form for people if they missed a required fields. Any and all fields that are missed show another form field, and those that have been done properly show as normal text.

This script has been made for a 3 field form but if you have more fields that require validation you will have to add those in yourself. Make sure that you match up the input NAMES porperly to the variables used or vice versa.

This script is provided for free but, I request that the copyright notice be kept in tact and in place.


[color #007700]
Code:
<?php
[/color]
Code:
#########################################################
#                                                       #
#
[color #770000]
Code:
PHP Form Validation/Mailer V-1.0
[/color]
Code:
                      #
#                                                       #
# http://www.mainframe-webdesign.com                    #
# Copyright ¬ 2003. All Rights Reserved.                #
#                                                       #
# This Copyright notice must stay in tact with script.  #
#########################################################
[color #000077]
Code:
/************************************************************
This script is for form validation to determine if all fields
have been filled out and if the email address supplied is of
valid syntax. If all statements return true then the form
is emailed to the specified recipient.

This script's main feature is that is replicates your form
for people if they missed a required fields. Any and all
fields that are missed show another form field, and those
that have been done properly show as normal text.

You will have to configure this to suit your needs. You will
also have to make sure that any input field names and hidden
field names reflect what you have on your form.
************************************************************/
[/color]
Code:
##########################################################
/* First - Check to see if the email is of Valid Syntax */
##########################################################
[color #007700]
Code:
$evalid=2;

if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$",$_POST["email"]))
{
	$evalid=1;
}

else
{
	$evalid=2;
}
[/color]
Code:
###############################################################################################################
/* Second - Check to see if all fields have been filled in and make sure $evalid=1.
If a field is not filled in or an E-Mail address is of invalid syntax ($evalid=2) then show the field again. */
###############################################################################################################
[color #007700]
Code:
if (eregi ("^$", $_POST["name"]) || eregi ("^$", $_POST["message"]) || $evalid =='2')
{
	echo "<FORM NAME=\"contact\" ACTION=\"mail.php\" METHOD=\"POST\">\n";
	echo "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"5\" WIDTH=\"401\">\n";
	echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"3\">ERROR!</TD></TR>\n";
	echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"3\">Please fill in the appropriate field(s)<BR><BR></TD></TR>\n";
	if (eregi ("^$", $_POST["name"]))
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">Name:</TD><TD ALIGN=\"LEFT\" VALIGN=\"TOP\"><INPUT TYPE=\"text\" NAME=\"name\" SIZE=\"25\" MAXLENGTH=\"50\"></TD></TR>\n";
	}

	else
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">Name:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\" COLSPAN=\"2\">" . $_POST["name"] . "</TD></TR>\n";
		echo "<INPUT TYPE=\"hidden\" NAME=\"name\" VALUE=\"" . $_POST["name"] . "\">\n";
	}

	if ($evalid == '2')
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">E-Mail:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\"><INPUT TYPE=\"text\" NAME=\"email\" SIZE=\"25\" MAXLENGTH=\"50\"></TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">Invalid E-Mail Syntax</TD></TR>\n";
	}

	else
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">E-Mail:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\" COLSPAN=\"2\">" . $_POST["email"] . "</TD></TR>\n";
		echo "<INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=\"" . $_POST["email"] . "\">\n";
	}

	if (eregi ("^$", $_POST["message"]))
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\">Message:</TD><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"2\"><TEXTAREA NAME=\"message\" COLS=\"50\" ROWS=\"15\"></TEXTAREA><BR><BR></TD></TR>\n";
	}

	else
	{
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\">Message:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\" COLSPAN=\"2\">" . $_POST["message"] . "<BR><BR></TD></TR>\n";
		echo "<INPUT TYPE=\"hidden\" NAME=\"message\" VALUE=\"" . $_POST["message"] . "\">\n";
	}


	echo "<TR><TD>á</TD><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"2\"><INPUT TYPE=\"SUBMIT\" VALUE=\"SEND\" onclick=\"contact.submit(); this.disabled = true; contact.RESET.disabled = true;\" CLASS=\"forminput\"><IMG SRC=\"/spacer.gif\" WIDTH=\"10\" HEIGHT=\"1\" ALT=\"\"><INPUT TYPE=\"RESET\" VALUE=\"CLEAR\" NAME=\"RESET\"></TD></TR>\n";
	echo "</TABLE>\n";
	echo "</FORM>\n";

}
[/color]
Code:
######################################################################################################
/* Third - If all fields are filled out and the E-Mail is of valid syntax continue to mail the form */
######################################################################################################
[color #007700]
Code:
else
{

	$name = $_POST["name"];
	$email = $_POST["email"];
	$message = $_POST["message"];

	$to = "email@site.com";

	$subject = "Site message from: $name";
[/color]
Code:
###############################################################
/* Fourth - This is how you format how you receive the email */
###############################################################
[color #007700]
Code:
	$body  = "Name: $name\r\n";
	$body .= "E-mail: $email\r\n\n";
	$body .= "Message: $message\r\n";

	$headers .= "From: ".($name)."<".($email).">\n";
[/color]
Code:
##############################################################################################
/* Fifth - This shows a confirmation of what the user wrote and what the recipient will see */
##############################################################################################
[color #007700]
Code:
	if (@mail ($to, $subject, $body, $headers))
	{
		echo "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"5\">\n";
    		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"2\">Thanks!</TD></TR>\n";
    		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"TOP\" COLSPAN=\"2\">Your message was sent successfully<BR><BR></TD></TR>\n";
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">Name:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">$name</TD></TR>\n";
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">E-Mail:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">$email</TD></TR>\n";
		echo "<TR><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">Message:</TD><TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">$message</TD></TR>\n";
		echo "<TR><TD ALIGN=\"LETT\" VALIGN=\"MIDDLE\" COLSPAN=\"2\"><INPUT TYPE=\"button\" VALUE=\"Finished\" onclick=\"window.location.href('main_page.html');\"></TD></TR>\n";
		echo "</TABLE>\n";
	}
[/color]
Code:
####################################################################
/* Sixth - This will show if there was an error sending the email */
####################################################################
[color #007700]
Code:
	else
	{
		echo "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"5\">\n";
    		echo "<TR><TD>Error!<BR><BR></TD></TR>\n";
		echo "<TR><TD>There was an error sending your form message. Please try again at a later time.</TD></TR>\n";
		echo "</TABLE>\n";
	}
}
[/color]
Code:
###########
# END PHP #
###########
[color #007700]
Code:
?>
[/color]

Hope this helps!


NATE

[img http://www.mainframe-webdesign.com/tt/mainframe.gif]
http://www.mainframe-webdesign.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top