PDA

View Full Version : php form mailer tut



Dave
March 9th, 2007, 08:35 AM
Hey guys,

I did the tut here and it works great! Just wondering, how can i make it so the fields name, and email are required fields?

http://www.kirupa.com/web/form_mailer.htm

hamza84
March 9th, 2007, 11:43 AM
<html>
<head>
<title></title>
</head>
<?php
function display_form(){
?>
<form action="<?php $_SERVER[PHP_SELF]; ?>" method="post">
Your Name: <input type="text" name="test"><br>
Your Email: <input type="text" name="email"><br>
Your Message:<br> <textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
<?
}
?>
<body>
<?php

if(isset($_POST['submit']))
{
if(empty($_POST['test']) || empty($_POST['email']))
{
echo "Name and email should not be left empty";
display_form();
return;
}

$to = "ADD YOUR EMAIL HERE";
$subject = "Results from your Request Info form";
$headers = "From: Form Mailer";
$forward = 0;
$location = "";

$date = date ("l, F jS, Y");
$time = date ("h:i A");

$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

foreach($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}

mail($to, $subject, $msg, $headers);

if ($forward == 1)
header ("Location:$location");

else
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

else
display_form();
?>
</body>
</html>


Its still very basic. You will need to add email and message checks.