Simple Contact us form in PHP

Most of the web sites have a contact us form which takes the user input and send it to mail box. Small web sites can use the example provided in this article and modify it according to their requirements.

Here is the complete code to send emails.


Code:

<?php ob_start();
?>
<!-- Add your header here -->
<html>
<head>
      <title>Contact Us</title>
</head>
<body>

<?
  //set up varible
  $date=date('Y-m-d');   
  $to="webshiju@hotmail.com";     
  $subject="Contact us form on $date";
   
  $show_form=true;

  if ($_POST['action']=="Submit"){
   
    $email=$_POST['email'];
    $comment=$_POST['comment'];

   if(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3}))$', $email)){
   //user entered a wrong email address
       echo "<div align=\"center\">";
      echo "Error! Wrong Email";
      echo "</div>";
   }elseif(empty($comment)){
      echo "<div align=\"center\">";
      echo "Error! Enter comment";
      echo "</div>";
   }else{
       $message="Email : $email\n";
      $message.="Remote Address : ".$_SERVER['REMOTE_ADDR']."\n";
      $message.=$comment;
      mail($to,$subject,$message,"From:$email");
       header("Location:contactus.php?status=sent");
   }
}elseif($_GET['status']=="sent"){
       $show_form=false;
?>
    <br />
   <div align="center">
   <strong>Thank you for your message.</strong>
   <br />
    </div>
<?
}


if($show_form==true){
?>
<script language="javascript">
<!--

function isBlank(val) {
   if (val == null) { return true; }
   for (var i=0; i < val.length; i++) {
      if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n")) { return false; }
      }
   return true;
   }

function validate(theForm){
   if (theForm.email.value.indexOf('@',0)==-1 ||
       theForm.email.value.indexOf('@',0)== 0 ||
      theForm.email.value.indexOf('.',0)==-1) {
      alert('\nInvalid email address.');
      theForm.email.select();
      theForm.email.focus();
     return false
   }
   
   if(isBlank(theForm.comment.value)){
       alert('Please enter comment.');
      theForm.comment.select();
       theForm.comment.focus();
      return false;
   }
   
   return true;
}

-->
</script>
<?
   $email=$_POST['email'];
   $comment=$_POST['comment'];
?>
<form method="POST" action="contactus.php"  name="frm" onsubmit="return validate(this);">
<table width="90%" border="0" cellpadding="3" cellspacing="1">
<tr>
    <td colspan="2"><strong>Contact Us Form</strong>
   </td>
</tr>
<tr>
    <td width="30%">Enter Email</td>
    <td><input type="text" name="email" size="31" value="<?=$email;?>"></td>
</tr>
<tr>
    <td valign="top">Comments</td>
    <td><textarea rows="6" name="comment" cols="30"><?=$comment;?></textarea></td>
</tr> 
<tr>
     <td align="center" colspan="2">
    <input  type="submit" value="Submit" name="action">
    </td>
 </tr>
 </table>
</form>
<?
}
?>

<!-- footer which you may replace -->
</body>
</html>



Make sure that you are changing the varibles in this example especially $to vaiable which is your email address.


Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net