Developing a contact form

The Form

To send information from a web page to PHP you will need to create a form like the one below.

The PHP

When the form is submitted the data is sent to the server. To retrieve that information you use the predefined variable $_POST['fieldname']. When called, the variable will return the value of the field specified.

In this example we will use a new function. The new function, mail(), attempts to send an email and then returns TRUE if it was successful or FALSE if it failed. Below is list of the parameters that the function accepts.

bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]])

Instead of a contact form maybe you would want to put up a mail form. The diffrence would be that rather than going to a specified email account the user can now decide who it goes to. To do this we will need to change some of our code. In the PHP you will need to replace the email address in the to parameter to $_POST['to'] in the mail function.

You will also need to add the code below to the HTML form.

There may come a time when you will want to log some type of information about your web pages. In this example write values to a text file which are collected by our contact form.

chmod: In order for PHP to be able to write to a file, you will need to modify that file's permissions. To do this on the pegasus server you will need a client like SSH Secure Shell. You can learn more about chmod at this chmod tutorial site. On pegasus, you need to give everyone permission to write to the file.

We will use three new functions to write to a file fopen, fwrite, and fclose. These three functions do exactly what they sound like they do. They open, write to, and close the file. Before we can write to the file we need to make sure that the file we are going to write to is on the server and that the permissions are correct so we will have access to the file.

Example

Log File

Example Source