|
|
| Article |
Kishore
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 |
|
|
| Comments |
dotnetstarter Contributing Member

Joined: 19 Feb 2007 Articles: 0 Comments: 1
|
| Posted: Thu Jun 14, 2007 1:30 pm Post subject: |
|
|
Very good code sample. A very useful one
Any idea, how to get a vb.net version of this code?
Thanks |
|
|
| |
Page 1 of 1 |
All times are GMT
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
|
|