Using PHP to set up a Form
Provided by the Good News Web Designers Association

Return to Directory of Tutorials or GNWDA home page

   
Tim's .php3 form script
donated by Inigo Media Limited

This really is a classic for us and has been used in contact forms over 100 times over the last few years. The only things you have to remember are these.

  1. Your server must support php3
  2. You must put the script into the form page and a thank you / follow up page
  3. If you don't number the name values for the forms, you won't get any data through

Follow these instructions and you can't go wrong. Unfortunately we don't have the time to run support for the script and if you need help on hidden values, etc., it's best to refer to a manual on php!

Donate if you like this script
If you find the script helpful, we are asking that you consider making a donation to our favourite charity.

The Africa Fund was set up to help intermediate technology projects get started in towns in Uganda and Kenya. A donation of $20 would be most welcome, but by no means compulsory. See the site at: http://theafricafund.net

First Part: calling the script from the form
Replacing your old .pl or .cgi script is easy. Add the following at the top of your form:

<form action="http://www.yoursite.org/contact.php3" method="POST">
<input type=hidden name="admin" value="contact@yoursite.org">
<input type=hidden name="subject" value="Contact Form">

Place </form> at the end of your form table. Make sure that the <form></form> tags are either both inside or outside the table that you have your form in - or it might throw an html wobbly and not format properly.

You'll need to add numbers to the form name values to make the form process.

<input type="TEXT" name="26)Address1" size="32">

Two tips on this:

Firstly make sure that all the numbers follow sequence. The data passed to the script from the form will then be processed in the order of the numbers you put in the name value. If the numbers are out of sequence, that data will come out in the wrong sequence. If you leave a gap, say forgetting to put a number in - that data will not be picked up by the script. Even worse is if you don't put numbers in at all. In that case you won't get any data either!

Secondly, make sure that the name is one string - not something like "Contact Address", which is made up of two elements. It will process the first word and not the second - or at least that's our experience of it.

Second Part: Delivery, saying thanks and moving on
This is the part you put in the thanks page - it can be plain html, but the ending has to be saved as .php3

<?php
$mail= "From: $admin\n";
$mail.="To: $admin\n";
$mail.="Subject: $subject\n\n";
$track_vars=1;
while(list($key, $val) = each($HTTP_POST_VARS)) {
// print("<TR><TD> $key </TD><TD> $val </TD></TR> \n");
ereg("^[0-9]{1,3}.(.*)$", $key, $m) && $mail.="$m[1]: $val\n\n";
}
mail("contact@yoursite.org", $subject, $mail, "From: $admin\nReply-To: $admin");
// echo "<p>-</p>\n";
?>

We usually have something saying, "thanks for your request/information, we'll get that to you as soon as possible" but you could have it redirect to another page or whatever you want. We use a link back to the page we want them to go to straight under the 'thanks' bit.

If you have any comments, please email dan @ inigo.net - thanks!

Download this tutorial as a printable Word Document
or as a PDF File.

Want a tutorial
one how to set up
a form using CGI?

EXTRA TIPS

Add more to your forms using a JavaScript. Here are some free scripts from The JavaScript Source

Return to Directory of Tutorials or GNWDA home page