Hi all,
I've coded the following form mail code that will simply send an email upon filling up information but I need some enhancements to it which I'm not sure how to go about coding it.
Code:
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
<form id="form" name="form" method="post" action="contact.php">
<p>Name:
<input name="fname" type="text" id="fname" value="" />
</p>
<p>Subject:
<input name="fsubject" type="text" id="fsubject" />
</p>
<p>Email Address:
<input name="femail" type="text" id="femail" />
</p>
<p>
<input name="fcc" type="checkbox" id="fcc" />
CC Yourself
</p>
<p>URL:
<input name="furl" type="text" id="furl" value="http://" />
</p>
<p>Comments:<br />
<textarea name="fcomments" cols="50" rows="10" id="fcomments"></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
}
else {
$uself = 1;
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
// Retriving textfields values
$senderName = stripslashes($_POST['fname']);
$senderSubject = stripslashes($_POST['fsubject']);
$senderEmail = stripslashes($_POST['femail']);
$senderURL = stripslashes($_POST['furl']);
$senderComment = stripslashes($_POST['fcomments']);
// Domestics
$to = "[email protected]";
$subject = "[Website] " . $senderSubject;
$senderADDR = stripslashes($_SERVER['REMOTE_ADDR']);
$senderHOST = stripslashes($_SERVER['HTTP_USER_AGENT']);
// headers
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/plain; charset=US-ASCII\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$header .= "From: \"$senderName\" <$senderEmail>" . $headersep . "\r\n";
// check if user wants a copy of the email
if (isset($_POST['fcc']) == "true")
$header .= "Cc: " . $senderEmail . "\r\n";
// Specify default timezone
date_default_timezone_set("Asia/Singapore");
// Constructing email message
$message = "From........: " . $senderName; "\n";
$message.= "\nSite........: " . $senderURL; "\n";
$message.= "\nAddr........: " . $senderADDR; "\n";
$message.= "\nHost........: " . $senderHOST;
$message.= "\n\n";
$message.= "********************************************************\n";
$message.= $subject;
$message.= "\n";
$message.= "********************************************************\n\n";
$message.= $senderComment;
$message.= "\n";
if (mail($to, $subject, $message, $header))
echo "Message successfully sent!";
else
echo "Error, message not sent!";
}
?>
My questions:
1. How can I add validations to the input such that it will check and ensure that the required fields have been filled up upon submission of the form?
2. Assuming the required fields were not filled up, how can I display each individual error messages (example: "Name is required", "Email address is incorrect" etc.) on the same page and above the form itself
3. If all the fields have been filled up correctly and the form have been submitted, how can I display a note (example: "Thanks, you submission have been received") on the same page but this time, without showing the form itself.
Thanks!