Self-Submitting PHP Tell-A-Friend form
Tuesday, January 6th, 2009Want to put a “tell a friend” form on your website so visitors can easily tell their friends about your site? Below is the code for a self-submitting PHP tell-a-friend form (which could also easily be adapted as a standard contact form).
Simply change “domain” to your website URL, and paste the code where you want the form to appear on your site. Your may also want to customize the message displayed to the user after they submit the form (near the bottom of the code).
<?php
if (!$_POST['tellafriend']) {
$frm = $_SERVER['PHP_SELF'];
?>
<form name=”tellafriend” id=”tellafriend” action=”<?php echo $frm; ?>” method=”post”>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”260″>
<tr>
<th colspan=”4″>Tell A Friend</th>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
<tr>
<td width=”10″></td>
<td>Your Name:</td>
<td><input type=”text” name=”yourname” size=”20″ /></td>
<td width=”10″></td>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
<tr>
<td></td>
<td>Your Email:</td>
<td><input type=”text” name=”youremail” size=”20″ /></td>
<td></td>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
<tr>
<td width=”10″></td>
<td>Friend’s Name:</td>
<td><input type=”text” name=”friendname” size=”20″ /></td>
<td width=”10″></td>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
<tr>
<td></td>
<td>Friend’s Email:</td>
<td><input type=”text” name=”friendemail” size=”20″ /></td>
<td></td>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
<tr>
<td></td>
<td colspan=”2″>Add A Personal Message:</td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan=”2″><textarea cols=”26″ rows=”8″ name=”comments”></textarea></td>
<td></td>
</tr>
<tr>
<td></td>
<td colspan=”2″><input type=”hidden” name=”tellafriend” value=”tellafriend” /><input type=”button” name=”send” value=”Submit” onclick=”tellverify();”></td>
<td></td>
</tr>
<tr height=”4″>
<td colspan=”4″></td>
</tr>
</table>
</form>
<?php
} else {
error_reporting(0);
$siteaddress =”http://www.domain.com”;
$sitename = “Domain.com”;
$adminaddress = “tellafriend@domain.com”;
$date = date(“m/d/Y H:i:s”);
$headers = “From: tellafriend@domain.com <tellafriend@domain.com>”;
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$friendname = $_POST['friendname'];
$friendemail = $_POST['friendemail'];
$comments = stripslashes($_POST['comments']);
// Gets the IP Address
if ($REMOTE_ADDR == “”) $ip = “no ip”;
else $ip = getHostByAddr($REMOTE_ADDR);
ini_set(“SMTP”,”mail.domain.com”);
ini_set(“smtp_port”,”25″); //could also be 26
ini_set(“sendmail_from”, “tellafriend@domain.com”);
if ($comments) {
$comments2 = “Your friend $name also wanted to say: \n $comments”;
}
mail(“$friendemail”,”Your friend $name thought you might be interested in this site”,
“Greetings $friendname, \n
Your friend $name ($email) found our website, http://www.domain.com, and thought you may be interested in it.
$comments2
Logged Info :
——————————
Using: $HTTP_USER_AGENT
Hostname: $ip
IP address: $REMOTE_ADDR
Date/Time: $date”,$headers);
mail(“$adminaddress”,”Tell a friend”,
“Name: $name
Email: $email
Friend’s Name: $friendname
Friend’e Email: $friendemail
Logged Info :
——————————
Using: $HTTP_USER_AGENT
Hostname: $ip
IP address: $REMOTE_ADDR
Date/Time: $date”,$headers);
echo(“message to display on website here”);
};
?>

