Post by egbakaet :D on Apr 27, 2005 19:20:34 GMT
Here's a great form php code I created. Feel free to use it. Start by making a new file called contactform.php.
<?PHP
//Email form by egbakaet
//egbakaet@gmail.com
//Please leave this in tact. Thank you.
if(isset($_POST["send"])){
$name = $_POST["name"];
$people = $_POST["people"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$msg = $_POST["msg"];
if(empty($name)){
echo("-There was no name!
");
}
if(empty($subject)){
echo("-There was no subject!
");
}
if(empty($msg)){
echo("-There was no message!
");
}
if(!empty($name) && !empty($email) && !empty($subject) && !empty($msg)){
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: ".$email."";
$messageproper =
"This message was sent from:\n" .
"$email\n" .
"------------------------- Email -------------------------\n\n" .
"Name: $name\n" .
"Email: $email\n" .
"Message: $msg\n" .
"\n----------------------------------------------------------\n" ;
mail("$people", $subject, $messageproper, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.02" );
echo( "Thank you <b>$name</b>, your email was sent." );
}
}
?>
The code above submits the form. The form can include html in it. Now to the form itself:
<form action="contactform.php" method="post"><font face="Verdana">
Name:</font>
<input type="text" name="name" size="35">
E-mail:
<input type="text" name="email" size="35">
Who to Send to:
<input type="text" name="people" size="35">
Subject:
<input type="text" name="subject" size="35">
Message:
<textarea name="msg" rows="8" cols="42"></textarea>
<input type="submit" name="send" value="Send Email"> <input type="reset" value="Reset" >
</form>
That's the form. Put the above into the <BODY> section of your php document. Now for the javascript validation (OPTIONAL). This code uses the names of the fields (marked in red above ) to check to see that they're not empty (Goes in <HEAD> section).
<script language="JavaScript">
<!--
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("name", "email", "people", "subject", "msg");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Your Name", "Your Email", "Who to Send to", "Your Subject", "Your Message");
// dialog message
var alertMsg = "Please complete the following fields:\n";
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " • " + fieldDescription + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " • " + fieldDescription + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " • " + fieldDescription + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " • " + fieldDescription + "\n";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>
The above code has two features: It checks the name of the field (marked in red) and prints an alert to advise you of missing fields. You can have custom messages for the error by editing the fields in green in the code above. Be sure to keep the order consectuive for example:
CORRECT:
"Name", "email"
"Your name", "Your email"
INCORRECT:
"Name", "email"
"Your email", "Your name"
If you would like to have this optional javascript validation, simply change:
<form action="contactform.php" method="post">
to:
<form action="contactform.php" method="post" onsubmit="return formCheck(this);">
In the form code. Hope this helps a lot of people. I might add another tutorial later on explaining how to add selects and options (very easy). Make sure the file name is the same as the form action or it won't work. If you experience any problems, let me know. This form uses html so be careful! Enjoy! ;D
Written by egbakaet (egbakaet@gmail.com)
<?PHP
//Email form by egbakaet
//egbakaet@gmail.com
//Please leave this in tact. Thank you.
if(isset($_POST["send"])){
$name = $_POST["name"];
$people = $_POST["people"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$msg = $_POST["msg"];
if(empty($name)){
echo("-There was no name!
");
}
if(empty($subject)){
echo("-There was no subject!
");
}
if(empty($msg)){
echo("-There was no message!
");
}
if(!empty($name) && !empty($email) && !empty($subject) && !empty($msg)){
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: ".$email."";
$messageproper =
"This message was sent from:\n" .
"$email\n" .
"------------------------- Email -------------------------\n\n" .
"Name: $name\n" .
"Email: $email\n" .
"Message: $msg\n" .
"\n----------------------------------------------------------\n" ;
mail("$people", $subject, $messageproper, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.02" );
echo( "Thank you <b>$name</b>, your email was sent." );
}
}
?>
The code above submits the form. The form can include html in it. Now to the form itself:
<form action="contactform.php" method="post"><font face="Verdana">
Name:</font>
<input type="text" name="name" size="35">
E-mail:
<input type="text" name="email" size="35">
Who to Send to:
<input type="text" name="people" size="35">
Subject:
<input type="text" name="subject" size="35">
Message:
<textarea name="msg" rows="8" cols="42"></textarea>
<input type="submit" name="send" value="Send Email"> <input type="reset" value="Reset" >
</form>
That's the form. Put the above into the <BODY> section of your php document. Now for the javascript validation (OPTIONAL). This code uses the names of the fields (marked in red above ) to check to see that they're not empty (Goes in <HEAD> section).
<script language="JavaScript">
<!--
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("name", "email", "people", "subject", "msg");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Your Name", "Your Email", "Who to Send to", "Your Subject", "Your Message");
// dialog message
var alertMsg = "Please complete the following fields:\n";
var l_Msg = alertMsg.length;
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " • " + fieldDescription + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " • " + fieldDescription + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " • " + fieldDescription + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " • " + fieldDescription + "\n";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>
The above code has two features: It checks the name of the field (marked in red) and prints an alert to advise you of missing fields. You can have custom messages for the error by editing the fields in green in the code above. Be sure to keep the order consectuive for example:
CORRECT:
"Name", "email"
"Your name", "Your email"
INCORRECT:
"Name", "email"
"Your email", "Your name"
If you would like to have this optional javascript validation, simply change:
<form action="contactform.php" method="post">
to:
<form action="contactform.php" method="post" onsubmit="return formCheck(this);">
In the form code. Hope this helps a lot of people. I might add another tutorial later on explaining how to add selects and options (very easy). Make sure the file name is the same as the form action or it won't work. If you experience any problems, let me know. This form uses html so be careful! Enjoy! ;D
Written by egbakaet (egbakaet@gmail.com)