Hello everyone,
I have a contact form that works great, at least it did for what I needed up till now...Now I need to add optional fields to it and also a dropdown with 3 choices and I have no idea on how to do this...
I will provide my PHP and HTML below, some help will be highly appreciated.
PHP :
<?php
function validateEmail($value){
return preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $value);
}
if ( isset($_POST['last']) && $_POST['last']!="Name" && isset($_POST['email']) && $_POST['email']!="Email" && isset($_POST['message']) && $_POST['message']!="Message" ) {
if ( validateEmail($_POST['email']) ) {
$destination="My EMAIL";
$subject="Contact Form";
$mailMessage.="<dt><strong>Name:</strong></dt><dd>".$_POST['last']."</dd>";
$mailMessage.="<dt><strong>E-mail:</strong></dt><dd>".$_POST['email']."</dd>";
$mailMessage.="<dt><strong>Message:</strong></dt><dd>";
$mailMessage.=nl2br($_POST['message'])."</dd></dl>";
$mailMessage = utf8_decode($mailMessage);
$mailFrom=$_POST['email'];
$mailHeader="From:".$mailFrom."\nReply-To:".$_POST['name']."<".$mailFrom.">\n";
$mailHeader=$mailHeader."X-Mailer:PHP/".phpversion()."\n";
$mailHeader=$mailHeader."Mime-Version: 1.0\n";
$mailHeader=$mailHeader."Content-Type: text/html";
if ( mail($destination,$subject,$mailMessage,$mailHeader) )
{
echo "<center><p style='color:#339966; font-family:Calibri;'> Thanks, got it! </p></center>";
}
else echo "<center><p style='color:#339966; font-family:Calibri;'> Required fields are missing! </p></center>";
}
else echo "<center><p style='color:#339966; font-family:Calibri;'> Invalid email! </p></center>"; //EMAIL VALIDATION ERROR
}
else echo "<center><p style='color:#339966; font-family:Calibri;'> Tsk..Tsk..Tsk! </p></center>"; //VARS ERROR
?>
And my HTML:
<form id="myForm" method="post" action="contact.php" >
<input type="text" value="Name" id="last" name="last" class="fields" onFocus="if(this.value == 'Name') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Name';}"><br/>
<input type="text" value="Email" id="email" name="email" class="fields" onFocus="if(this.value == 'Email') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Email';}"><br/>
<textarea cols="20" rows="5" id="message" name="message" class="fields" onFocus="if(this.value == 'Message') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Message';}">Message</textarea><br/>
<center><input type="image" src="images/btn_trimite.png" border="0" name="submit" alt="Send"></center>
</form>