dev-exchange.com logo

dev-exchange.com latest topics RSS feed

Book mark: Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your delicious account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your digg account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your blinklist account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your reddit account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to Dzone Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your furl account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your stumble account Add http://www.dev-exchange.com/cms_view_article.php?aid=6&start=0 to your Yahoo myweb account
Article Menu
Google
ColdFusion MX
PHP
ASP
ASP.Net
XML-XSLT
JavaScript
SQL Server
AJAX
 


 
Article
Kishore
Contributing Member
Contributing Member

Articles: 10 Comments: 3
 Posted: Thu May 31, 2007 2:50 pm

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="test@somedomainnamehere.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.


Download full source code at:
http://www.dev-exchange.com/article/contactus.zip
Rating: 4.00/5.00 [1]

Comments
dotnetstarter
Contributing Member
Contributing Member


Joined: 19 Feb 2007
Articles: 0
Comments: 1
 Posted: Thu Jun 14, 2007 1:30 pm  Post subject:

Boo hoo!

Very good code sample. A very useful one
Any idea, how to get a vb.net version of this code?

Thanks
Rating: 0.00/5.00 [0]
 

Page 1 of 1
All times are GMT

Jump to:  
You cannot post articles in this chapter
You cannot edit your articles in this chapter
You cannot delete your articles in this chapter
You cannot rate articles in this chapter

You cannot post comments in this chapter
You cannot edit your comments in this chapter
You cannot delete your comments in this chapter
You cannot rate comments in this chapter


© 2008 dev-exchange.com
Powered by phpBB. Theme DEVPPL.