SEO Packages

Formmailer in PHP with required fields

October 29, 2011EditorPHP, Web DevelopmentComments Off

Ok, in this tutorial I’m going to show you how to write a formmailer with required fields in php. First off we need a form to use with this tutorial so use the code below and save it as form.php.








Name
Email
Age
message


Now open notepad and save this file as send.php and write the following.

mail(“”, “”, “,”);

?>

That’s the mail command, All we have to do is fill it in now. In the First set of quotes put the email address of who get the email. In the second set of quotes put the subject of the email and in the third set of quotes put the message of the email. If you want what people put in the form you will have to put the fields name with a $ in from of it like so.

mail(“webmaster@domain.com”, “Test Form”, “
Name: $name
Email: $email
Age: $age
Message: $message,”);

?>

$name indicates that what ever is put in the field with that name is going to be written there. $field name could be what ever you set it to in the html form code. Now we need a thank you message for people to get after then send the form.

PRINT “Thank you $name for submitting the form.”;
PRINT ” We will get back to you asap.”;

mail(“webmaster@domain.com”, “Test Form”, “
Name: $name
Email: $email
Age: $age
Message: $message,”);

?>

Now upload send.php to your server and try that in your browser (example: http://yourserver.com/send.php). Make sure your server support php scripting first. You should get a message that says Thank you for submitting the form. We will get back to you asap. Now we need to make is so people can’t submit the form with out putting info in.”Required fields”

if (!($name && $email && $age && $message)) {echo “You must complete the form to send it”; } else {

PRINT “Thank you $name for submitting the form.”;
PRINT ” We will get back to you asap.”;

mail(“webmaster@domain.com”, “Test Form”, “
Name: $name
Email: $email
Age: $age
Message: $message,”);

}

?>

 

Ok try that now but this time don’t type any thing in just click on Send Mail. You should get a message You must complete the form to send it. Try it again but this time put all the info in you should get the thank you message again. But if you want a different message for each fields use the code below

if (!$name) {echo “You must enter your name, Thank you”; } else {

if (!$email) {echo “We need your email address so we can get back to you.”; } else {

if (!$age) {echo “Don’t forget to let use know your age.”; } else {

if (!$message) {echo “You have to type in a message, No if’s N’s or buts about it.”; } else {

PRINT “Thank you $name for submitting the form.”;
PRINT ” We will get back to you asap.”;

mail(“webmaster@domain.com”, “Test Form”, “
Name: $name
Email: $email
Age: $age
Message: $message,”);

}

}

}

}

?>

Well that’s how you do a formmailer with required fields in php. 

m4s0n501

Related Posts

Comments are closed.