Llamasery.com Home  
 
Server-side Starter Tutorial: - Part 6 of 6
Alfie the alpha_geek: Llamasery guru-in-waiting Tutorials Purpose

Starter Tutorials

Tell-a-Friend
Tell-a-Friend +
The 'Contact Form'
php Includes
Simple Themes
MySQL database 101
Comments System
Text Files

Intermediate Material

Articles/Tutorials

Mixing php and html

What you will have noticed by now is that (apparently) nothing happens after you press send with the form. It's time to extend our php script to let you know (and your tell-a-friender) that something really happened. For now, we'll use the simple method of mixing html into our php script.

This is an obvious html snippet that we can add into our script:

<html>
<head>
<title>Thank you</title>
</head>
<body>
Thanks for telling a friend
</body>
</html>

All we need to do is 'finish' the php code by using the ?> characters to signal the end of scripting, then add the html stuff.

And here's the further modified script

<?
$mail_to = $_POST['friend_email'];
$sender_email = $_POST['sender_email'];
$sender_name = $_POST['sender_name'];
$mail_from = "From: webmaster@yourdomain.tld";
$mail_subject = "Site recommendation";
$mail_body = "Your friend ";
$mail_body.= $sender_name;
$mail_body.= " has recommended http://www.yourdomain.tld";
mail("$mail_to","$mail_subject","$mail_body","$mail_from");
// now build the mail to the friend
$mail_from = "From: webmaster@yourdomain.tld";
$mail_to = $sender_email;
$mail_subject = "Thank you for your recommendation";
$mail_body = "Thank you for sending a recommendation to ";
$mail_body.= $friend_email;
$mail_body.= " about our site at http://www.yourdomain.tld.";
mail("$mail_to","$mail_subject","$mail_body","$mail_from");
?>
// now add some thank you words
<html>
<head>
<title>Thank you</title>
</head>
<body>
Thanks for telling a friend
</body>
</html>

Save this new version as taf.php. Upload taf.php and taf.html (your changed form) to some testing space on your web server, and navigate to taf.html. Try your new tell-a-friend.

What did you learn?

Apart from learning that php scripts require exact syntax and grammar so that they work, you have learned a number of things in this tutorial:

  • How to retrieve (and manuipulate) data sent from a form.
  • How to generate 'complex' strings in php by adding stuff to existing strings.
  • How to use e-mail from within php.
  • How to mix html and php in the same script.
Just about over

There are a number of other things we could do to this basic script, but the point of the whole tutorial was to give you a feel for some simple, useful, scripting - not to create a monster application. Some of the things we could/should do to make the whole thing more useful/robust include:

  • Checking that 'something' has been entered for each field in the form.
  • Checking that the e-mail addresses entered do make sense (and even exist) before sending.
  • Using an 'included' thank-you page instead of a little bit of html code.
  • Adding e-mail addresses and form use information to a log file on your server.
  • Preventing html tags and weird stuff in form fields

... but all that is for another day (unless you want to go ahead and do it yourself).

The end

printer friendly pageAssuming your typing is flawless, and my scripting was flawless, that ought to be all there is to this tutorial. If you have serious problems with it, let me know and we'll try to correct or explain what wasn't working for you.


Average Rating: 4.4 out of 10 from 50 voters.

«  previous