This is not a How To tutorial, it is rather a Description to Forms Basics.
The reason I’m posting it here is because I believe that it will be lost if I post it in the forms forum.
Forms are most important for communication between you and your visitor. They allow your visitor to type in all the info which then arrives to you. The way that you create a form page is pretty well explained in the BV tutorials which are here:
Tutorial nr.1 http://www.vodahost.com/DemoDemo/bvt/custom_bluevoda_basicform.htm
Tutorial nr. 2 http://www.vodahost.com/DemoDemo/bvt/custom_bluevoda_formelements.htm
Here I’d like to explain a little the mechanisms used to send the form data to you, examine how you can achieve this, and give some tips.
1. The “Action” and the PHP script.
When you have created a form, and you double click it, you will note in the properties window, a field called “Action”. Action is what the form is supposed to do when you click the Submit Button.
If you want the form info mailed directly to you, you can set the action in:
mailto: firstname.lastname@example.org . Usually this kind of action also requires a plain text encoding (encoding type also in the form properties window).
The most common problem that could arise when you go this way, is that your visitors email client
(Outlook Express in most cases) is not correctly set up. If this is the case, instead of submitting the form info, your visitor will only see Outlook Express open up in his screen, but completely empty. as if had to send a normal email. And, you can do nothing about this, because it’s HIS settings that don’t allow the form to send the info.
To most reliable way to overcome this and really submit the info, is to use a PHP script.
PHP is a powerful scripting language. When you have a PHP script to process the form info, the mail to you is created and sent by the script itself and not by Outlook Express (or whichever the mail client might be). First of all, the form has to invoke the script: that is why the “Action” field in the form properties has to be filled in with the name of the php script, as you will upload it on your server. For this tutorial purposes, we shall call it “contact.php” but you can also call it whatever you like, as “action.php”, “choice.php”, “mailme.php” etc.
So, when you click on Submit, the form transmits the info to the script and “activates” it. The script “grabs” the info, makes all that is supposed to do, and then creates and sends an email to your inbox. Usually most php form scripts, work with the “multipart / form date” encoding, which is another option in your forms properties.
Please note that the form submits the info together with the names of your fields. So for example it will submit not only the name “George”, but also the field name, so it submits “Name: George”, “Email: email@example.com”, etc. This way the script can see not only the value of each field, but also the field name. This is necessary if the script has to perform error checking, else called validation. We shall see about it later on.
The action of the script is to create and send you the email. Lets see how this is done by examining the most used script, provided by Vodahost and Pablo, well known Forum Moderator:
( look at: http://www.vodahost.com/vodatalk/forms/933-another-forms-question.html post nr. 7)
$email = $HTTP_POST_VARS[email]; Here the script grabs the senders email, if filled in
$mailto = "firstname.lastname@example.org"; Here you give the scriptyour email address
$mailsubj = "Feedback form"; The email subject
$mailhead = "From: $email\n"; Define the “from” header
$mailbody = "Values submitted from web site form:\n"; Text before the values, a reminder.
while (list ($key, $val) = each ($HTTP_POST_VARS)) For every and all the values create
$mailbody .= "$key : $val\n"; a text with the field names and all the values
mail($mailto, $mailsubj, $mailbody, $mailhead); Send the email
In some other cases, usually when validation is required, the script first grabs the single fields and then creates the mailbody with a part that looks like:
$pfw_message = " Values submitted from web site form:\n"
. "Name: $Name\n"
. "Last_name: $Last_name\n"
. "Email: $Email\n"
. "Address: $Address\n”
. "City: $City\n"
. "Country: $Country\n"
. "State: $State\n"
. "Telephone: $Telephone\n"
. "Fax: $Fax\n"
. "Text: $Text\n";
*****($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
Which is more or less the same.
More lines of code can be implemented in the script, to “grab” also additional information like server date and time, IP address of the visitor etc.
2. Field validation
It is some times useful to make sure that your visitors have filled in certain fields. To do so, the first thing the script must do, is separate the info it receives: it is done with a code like:
@$Name = addslashes($_POST['Name']);
@$Email = addslashes($_POST['Email']);
and this is repeated for all the required fields. Validation can be of different types, most common:
1. For empty fields: with a code like: if (strlen($Email) == 0 ) to check if email field is empty
2. For a truelike email: if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $Email))
At this point, the script has to give your visitor a message if some of the fields are empty or the email is not true like. If everything is ok, then the script creates the mail and sends it to you. Now it must take your visitor to a Thank You page, to let him know that everything went ok.
3. Combining things together
So you have created your form, and have also prepared your script, which as we said, we shall call “contact.php” for the purposes of this tutorial. Well, now you have to decide how to put things together.
If you go for Pablo’s script described above, you can combine the php script and the Thankyou (or Confirmation) page. Indeed, html and php can easily be combined together. This is done even more easily using BV: create your thankyou page as any other page, then insert the php, set the “when published” extension for the page in “php” (from page properties), save the page and publish it. It is all explained very clearly from Pablo in
http://www.vodahost.com/vodatalk/forms/933-another-forms-question.html post nr. 7
However, I recently noticed an increased request for field validation. When you do that, you would also like to have different error pages, each for every error, and make the script redirect the visitor to the appropriate error page if an error occurs. This is done easier if you keep the visual part and the processing part separate. different error messages for every kind of error. To accomplish this, you must create an Error Page Template, This is my personal choice.
To check for an error your code will look like:
if (strlen($Email) == 0 ) check the $Email lenght to make sure that the field is not empty
header("Location: error_email.html"); error_email.html is the error page relative to
exit; wrong email input
Other types of syntax are appropriate for other types of validation.
To have a look at a script that makes field validation, and also file uploading, please go to
http://www.vodahost.com/vodatalk/forms/2887-auto-response-e-mail-registry-form.html#post11943 Post nr.3
and also feel free to try the form that is there as well as the auxiliary error and thank you pages.
In this case you must upload the php script as it is, on your server. If you find a script somewhere, copy it, and paste it in Notepad. Goto to File, Save as, then chose File Type “All files” and save it as “contact.php” . Upload it on the server.
Important: it’s a good rule to upload (or publish) everything in the same directory, though it is perfectly possible to have the files also in different folders. Since this is a bit more difficult, just stick to this rule.
4. File Uploading and Database Updating
Forms are often used for File Uploading and Database Writing. This feature exceeds the purposes of this tutorial. Just keep in mind that it can be done. If you want to create such a form, post on the forum and you will get the help you need.
5. SMTP Autentication. Must Read !!
Vodahost has a very strict antispam policy. For this reason, the mail() function which is used on ALL simple scripts, is disabled. So, to override this,
Your email address that you put in your script, MUST be on your Vodahost account, not an external one.
This is also the reason why a script with no SMTP authentication will not be able to send an autoresponder message to the form submitter.
TIP ! You can set an email account on your domain, specifically for the form to work with. Then, in your CP go to Mail, and if you wish, you can create an autoresponder for that email account. This way you can send a simple confirmation mail to your visitor assuring him that you have received the form info.
6. Common Errors
Some of the most common errors are:
1. Creating the form, then forgetting to edit the fields. I’ve seen many forms with similar, f.e. text fields, with the same default name that BV gives them, i.e. T1. The same for other fields.
2. Using Pablo’s script and naming the email field f.e. ”Email address” whilst the script itself requires ”email”.
3. Setting the action in something that doesn’t exist. Very common typing errors, commas instead of points etc.
4. Classic error: the Submit button outside the form area. It won’t work.
5. Radio Buttons used for making choices HAVE to be logically grouped. Very often they are independent, so once you click them they can’t be unclicked anymore.
6. Text area used instead of a combobox: unless you click on some of the options, the variable value will be null
7. The send to email address: as said above, has to be in your Vodahost account.
I hope this post will help new friends in the Forum understand the basics about forms and save themselves some time.
for everything you might need please post in the Forms forum.