
| Forms are best layed out in tables. Using a transparent background gif file makes controlling the cell widths very easy. If you don't have one, you can download one from here. (spacer.gif) Right click on the file, select "Copy to Folder" or "Save link as", depending upon your Browser. |
| The form we will use is a pretty straightforward 'name and address'-type form, but the theory is good for any form. The form is shown below - the HTML source code can be downloaded from here. While you're there, it will save time if you download both formail.htm and formail.php - you might like to look at both later. (I do not recommend trying to pick out the php code from the source code of this document - far easier to download the files and examine them in isolation). Right click on the file, select "Copy to Folder" or "Save link as". |
| The main purpose of these two files (formail.htm & formail.php) is to demonstrate the ability of php to replace the need for a cgi (Common Gateway Interface) script installed on the server - cgi installations can prove tricky even with direct access to the server, and impossible if all you are allowed to do is upload web pages.
There are comments in the php source code, but it's probably worthwhile adding a few, section by section. The code is in blue, the comments in red. ------------------------------------------------------------------------------------------------ |
|
Usual headers for any HTML page <HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY BGCOLOR="FFFFFF"> <center> All php starts with <?php and ends with ?> - there are variations but these are the clearest, imho. <?php Set up the email message header by initiating the variable 'message', terminating the first line with \n for new line. A second line is then added using .= again terminating with \n $message = "Thank you for using our on-line form.\n"; $message .= "Your entries were as follows -\n"; On our form, the first box was created using <INPUT TYPE="text" NAME="uname" SIZE=24 VALUE=""> NAME being set to uname. When the form is submitted, all NAMEs from the form are passed to the php file, along with their VALUEs which have been entered by the user. uname is the NAME of the variable, $uname is it's VALUE At the risk of labouring a point, $anything is the value held in anything - put another way - LET a = 5 PRINT (2 x $a) would produce the answer 10, but I digress. if ($uname) is one way of saying "Does $uname have a value?" Another way could be if ($uname != "") in other words is not equal to nothing. Side by side double quotes does not have the same meaning as " " - the latter means "Does it contain a space?" which is different from containing nothing at all. The next line contains an opening brace { - three lines below that, is a closing brace }. The two lines in between will be used if $uname has a value. if ($uname) Check for Name input { Opening brace We now set up another variable which will give a little more meaning to the email when it is read. The line below says - the value held in $user becomes the word Name dash value held in $uname. $user = "Name - $uname"; Name - Fred Bloggs $message .= "$user\n"; Add this to the message using .= } Closing brace else if $uname has no value, as it was a required field, we'll start an error message to use later. { $error = "You did not enter your name<br>\n" ; Error if no Name input on form. } These next few sections are all very similar, although some do not add to the error message (they were not required fields on the form). if ($add1) Check first line of address { $addr1 = "Address - $add1"; Create a line for the email message $message .= "$addr1\n"; Add it to the message } else { $error .= "First line of address missing<br>\n" ; Add to Error if missing } if ($add2) Check second line of address { $addr2 = "Address - $add2"; Create a line for the email message $message .= "$addr2\n"; Add it to the message } else { $error .= "Second line of address missing<br>\n" ; Add to Error if missing } if ($add3) Check third line of address { $addr3 = "Address - $add3"; Create a line for the email message $message .= "$addr3\n"; Add it to the message } if ($add4) Check fourth line of address { $addr4 = "Address - $add4"; Create a line for the email message $message .= "$addr4\n"; Add it to the message } if ($zip) Check for Postal or Zip Code { $uzip = "Post/Zip Code - $zip"; Create a line for the email message $message .= "$uzip\n"; Add it to the message } else { $error .= "You did not enter your Post or Zip code<br>\n" ; Add to Error if missing } if ($tel) Check for telephone number { $utel = "Telephone - $tel"; Create a line for the email message $message .= "$utel\n" ; Add it to the message } if ($fax) Check for fax number { $ufax = "Fax - $fax"; Create a line for the email message $message .= "$ufax\n"; Add it to the message } if ($uemail) Check for email address { $umail = "Email - $uemail"; Create a line for the email message $message .= "$umail\n"; Add it to the message } else { $error .= "You did not enter your e-mail address<br>\n" ; Add to Error if missing } This is where we check $error to see if we have picked up any along the way. If the form was completed correctly, there will be none and a copy of the message will be sent to the person completing the form (and one to yourself of course). This time we can use if ($error == "") - that's double = with no space in between. A quick note on = $x = "Fred Bloggs" means "Let $x assume the value Fred Bloggs" $x == "Fred Bloggs" means "Does $x hold the value Fred Bloggs" a comparitive test, not a statement. if ($error == "") If Error contains nothing, display a message and send the email. { Everything from now to the closing brace will happen, if there were no errors. echo "Thank you for using our on-line form. echo or print will display on screen <br><br>A copy of your details will be emailed to you almost immediately.<br><br> <br>Please check the email and inform us of any errors as soon as possible. <br><br>Thank you."; End of screen message This is where we send a copy of the form details to the form filler. To actually use this routine, you would need to substitute YOUR NAME and your@email.address with your name and your email address so that the form filler knows what the email is all about just by reading the headers. DO NOT remove the < > brackets The command below is mail (the sendmail command) - $uemail (the address of the recipient) - Your Form Details (the Subject) - $message (the message we have built up throughout) - From: YOUR NAME <your@email.address> (the From field, to which he/she can reply). mail("$uemail", "Your Form details", $message, "From: YOUR NAME <your@email.address>"); Similarly below, but with From and recipient reversed - Substitute your@email.address with your email address - DO NOT remove any quote marks This is the bit that sends YOU the contents of the form mail("your@email.address", "Booking Form Details", $message, "From: $uemail"); BUT if there was a required field missing, and error would have been generated, in which case the following will apply - } else If Error does contain something, display a message and tell the user what's wrong. { print "Sorry, but the form cannot be sent until the fields marked with a red cross are completed - <br>\n"; $error will contain one line for each missing required field, telling the user precisely which one(s). print "$error<br>\n"; print "<br>\n"; print "<br>\n"; print "Please use your \"Back\" button to return to the form to correct the omissions. Thank you.<br>\n"; } ?> The end - close php <br><br> </center> </BODY> </HTML> --------------------------------------------------------------------------------------- Comments? Questions? Suggestions for Other Topics? Please email me. |