Creating a Contact Form with PHP

3 07 2007

Hello,

In what seems to be our first PHP tutorial I am going to talk about creating a form using CSS and XHTML and then sending the input values to an email address. But before we can start on this, lets get to work creating the basis on the form.

Creating the Form

Firstly to create our form I decided to use CSS as this brings more flexibility in designing and styling out forms than using the conventional table based layout

Firstly we need to create a form, to do this we use the <form> tag. Below is the structure that the form will take.

<form name=”contact_form” action=”sendform.php” method=”post”><br />

</form>

You will notice that we have a couple of values within the opening form tag. Firstly name assigns the name of the form. The action value is used so that when the form is submitted this is the page that the browser will go to while giving the method of post which is how the data will be received.

However no visible elements are displayed within the browser at the moment. To do this we will apply the following code within the <form> tag.

<label>Name</label>
<input type=”text” class=”text_input” name=”name” value=”"><br />

<label>Email Address</label>
<input type=”text” class=”text_input” name=”email” value=”"><br />

<label>Comments</label>
<textarea name=”comments” rows=”10″ cols=”50″></textarea><br /><br />

<input type=”submit” value=”Send”>

So from this we can see that there is the <label></label> tag. We use this so that the user can see what value they need to place within each <input>

The <input> tag is used to define a input.
Within this form we can see that we use 3 different types of input.

Text – this is used when a text data value needs to be entered into the field
Textarea – this is used when a large number of characters needs to be inputted
submit – this is used to submit the form so that the values can be used within the action value

Additionally ‘class’ is used for styling the input type and is used within the CSS which we will later cover. Furthermore we need to define each input field with a unique name which is entered within name=”" so that they can each be identified in receiving the values of the particular input field.

Now if we run this within the browser we will see that all of the form elements are displayed however there are no style elements associated with the form so the design of the form will be poor. So now we will go on to styling the form.

Styling the form

As I earlier said, I chose to style the form using CSS instead of using tables as CSS reduces the use of repetitive tags as all styles are defined by each class value.

Now firstly add the following code within the <head></head> section of the document, css comments are added within the file to demonstrate the effect that each class will have on the input value.

Note
/*comment text*/ indicates a CSS comment

<style type=”text/css”>

label {display: block;} /* make sure that the input will be displayed below the label */
.text_input {width: 450px;margin-bottom: 5px;} /* each input field with a class value of text_input will be 450px wide and will have a margin of 5px on the bottom */
textarea {width: 450px ! important;height: 150px;} /* each <textarea> tag will be 450px wide and have a height of 150px*/
</style>

We have now created the main elements of the form and styled it appropriately (although additional styling can be applied to make the form more inkeeping with the theme of your website).

We will call this page ‘contact.html’.
Now we will be going on to creating the PHP code that will actually send the input values to a particular email address.

Using the mail() function to send the form

We will now create a new page called ’send_form.php’
This will be the main core of the form as this page will be what actually sends the data that has been inputted in ‘contact.html’. But remember to keep ’send_form.php’ and ‘contact.html’ into the same directory.

You will need to add the following code within this new page.
As again with the CSS comments have been included within the code so that you can establish what each variables particular job.

Note:
// is also the comment code for PHP
$variable_name is the way variables are implemented within PHP

<?php

// receive the values from name=”” and give each a variable name
$name = $_POST['name']; // receive the name value
$email = $_POST['email']; // receive the email value
$comments = $_POST['comments']; // recieve the comments value

// add all of the required information which will be shown in the body of the email $message= “From $name using an email address of $email sent:<br><br>$comments”;

// Send the message
$send=mail(‘email address to’,'Contact Form’, $comments);

if($send){
echo ( “Thanks for contacting us $name” ); // if the email was sent give a thankyou message
}
else{
echo ( “There was an error with your request” ); // or if not give error message
}

?>

Now if you run contact.html and submit the form with all of the values entered you should receive an email containing all of the fields that you entered in the body of the email.

Well, I hope you enjoyed the first part of creating the form as in the next part of the contact form series we will be enhancing the form using validation and preventing spam.





CSS Positions

28 06 2007

How to properly position elements in CSS

CSS can be used to define the positions of particular items such as divs, images and spans. However many people are not aware about positioning elements so are not aware how useful they can be in any website. The main two positioning that are used are absolute and relative. I will discuss the use of both and how they are used in a common design environment.


Absolute Positioning

Firstly, when items have an absolute position, the item is independent to all other items within the page as it shows the exact position within the browser without relying on any other elements.

Example 1

<span style=”left: 100px;”>This text uses absolute positioning </span>

We can see from this example that the span tag is positioned 100px from the left of the browsers screen because we defined the value of left to be 100px.

The reason why this includes absolute positioning is because as it is the first tag to appear it in effect inherits a position of absolute

Note
If you only have a small amount of code it may be suitable to included it within a tag by using style=”"
Also if you would like to include absolute positioning within your code you will need to type within style=”", position: absolute;


Relative Positioning

However we can also use relative positioning. This is when the position of an element relies on another.

Example 2

<span style=”position: relative; left: 200px;”>This text uses relative positioning</span>

So from this example we can see that this element would be 200px from the left.

Using Absolute and Relative Positionings together

However in both examples we can not see the true effect of the positions, so we will combine both examples so that we can see how they are used in a common design environment.

Example 3

<span style=” left: 100px;”>This text uses absolute positioning</span>
<span style=”position: relative; left: 200px;”>This text uses relative positioning</span>

So in this example we can see that the text which has an absolute position is 100px from the left of the browser. We can also see that the text which uses relative positionings is 200px from the left of the absolute positioning text. So the relative positioning text would be 300px from the left of the browser as both left values need to be combined to find the total distance the the relative text is from the left of the browser screen.

Additionally you could change the left value to 50px:

<span style=”left: 50px;”>This text uses absolute positioning</span>
<span style=”position: relative; left: 200px;”>This text uses relative positioning</span>

The relative positioned text would still be 200px from the absolute positioning text however it would be 250px from the left of the screen as the absolute element’s left value has been decreased.

So we can see that when we use absolute positioning, its associated values are independent and the styles that are set only effect itself. However we can see that with relative positioning, its position is dependant on the absolute positioning as some of the styles set affect those which are positioned relatively from it. However please note that only the margins, paddings, and right/left/top/bottom values effect the relative position as other styles are not inherited unless defined within the code. We can see the effect this has if we remove ‘position: relative;’ or change the value of ‘relative’ to ‘absolute’

Well I hope you enjoy what seems to be our first CSS tutorial and I believe it is an important one introducing positions.