Creating a Simple Send to Friend Form Script in PHP



Learn How to Build a Form To Allow Users to Send Your Page to a Friend

By now we should have the basics down, so I will introduce a new little tid bit for helping you create a send to friend script.  We see these very often, and they can range in complexity, but this will give you that one key element that you will need (and probably having a hard time finding).

You know how to make a form and process it, but how do you capture the url of the page you are sending?  You could just put the website in the form, but to send your friend the exact page is actually pretty simple.

$link = $_SERVER['HTTP_REFERER'];

That's it.  That will capture the url of the page your sending and process it as "$link".  From here you will make your form the way you normally would, and wherever you want the link to show in the sending email, place the "$link" and your all set.

Here is the sample html form to use.

<form action="process.php" method="post">
<table>
<tr>
<td>
Your Name<br>
<input name="yourname" type="text" size="40">
<br>
Friends Name<br>
<input name="friendsname" type="text" size="40">
<br>
Your Email Address<br>
<input name="youremailaddress" type="text" size="40">
<br>
Friends Email Address<br>
<input name="friendsemailaddress" type="text" size="40">
<br>
Message<br>
<textarea name="message" cols="30" rows= "5"wrap="PHYSICAL"></textarea>
<br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>

Now we add the PHP to complete and process the send to friend script.  We will use the same theory that we applied to our earlier examples, only this time there is more than just one form field, so we will need to process all of them.

<?PHP
$uname = $_POST["yourname"];
$fname = $_POST["friendsname"];
$uemail = $_POST["youremailaddress"];
$femail = $_POST["friendsemailaddress"];
$message = $_POST["message"];
$link = $_SERVER['HTTP_REFERER'];

$to = "$femail";
$subject = "Check out this site I found";
$headers = "From: $uemail\n";

$message = "Hi $fname.  Check out this site I found online today.
$link
$message";

mail($to,$subject,$message,$headers);

?>

That's it.  Pretty easy stuff once you start getting the hang of it.  It all works the same way, just adding more form fields to the process, but it can seem a little intimidating sometimes.

 

Previous

Special Package Offer

    Discounted deal on all of our php contact form scripts as a bundle package