Category Archives: Best Practices

Using PHP to Handle HTML Forms

Right probably the moment most of you have been waiting for, getting input from users! fear not. This is not hard, in fact it is not as hard as i first imagined. Now i have said in the introductory pages that i am assuming some basic knowledge of HTML. So this would cover setting up forms etc.

If your not so confident with HTML then fear not its pretty straight forward and plus you could probably get something like Dreamweaver to do it all visually if you need to.

Right I hope you all know what a form is (if not, they are the text boxes etc that you fill in when you order something online etc)

OK enough babble, an example… Enter the following code into a new notepad file and save it as “form.php”.

The Form.


<form name="form1" method="GET" action="">

Email: <input type="text" name="email">

<input type="submit" value="Submit">


OK I will just explain a little more about the form, we have declared the form at the top using the <form> element, we have also set some attributes of the form, as in the way in which it will send data “method=GET” this means that we will send data via appending it to the URL or web address using querystrings – PHP automatically creates the $_GET array for you. This means any element on your form, once it is submitted will be available in the $_GET array. The same is true if you decide to send the form via POST. You will have a $_POST array to handle. The example below will clear this up.

Notice how we have not set the action element, we have just left it blank. This means that we will simply be sending the data to the same page it came from, whats called a postback. This means that we can put all our PHP code on the same page as the form. Not the most useful example, but i will expand it when we come to look at databases etc.

So all we are going to do is take the value entered into the form and make them into an email(mailto) link. So on the same page, lets tap in the following code:


if(isset($_GET['email'])) {

print ("Please send your email to <a href=\"mailto:$_GET['email']\">" . $_GET['$email'] . "</a><br>");



Ok what we have done is used a built in function of PHP to check if anything has been entered into the form of not. We use inset to set if the $email variable has been declared(created really) and if it has we print out some HTML with a link to the email address entered into the form and the actual contents of the form printed out.

Your end result should look something like this:

Notice how the actual email address is attached to the end of the URL in the address bar that is the query string. Because we have written the HTML output as a link when we click on the link it will initialize your email client as it should ready for an email to be sent to the specified address.

Getting Started with PHP part 4

** Please check the documentation on the php website if you are having problems with installation. Or email me and i’ll be glad to help out.

To open up IIS and see what I’ve been banging on about you will need to go to Control Panel – Administrative Tools – Internet Information Services(IIS).

** I recommend making a short cut to this on your desktop.
OK when you first open IIS it should look something like this(Once you have expanded all the branches to the Default Website Level)

Internet Information Services - Default View

The to create a virtual directory, Right Click on the Default Web Site and go to ‘new’ – ‘Virtual Directory’.

Internet Information Services - Virtual Directory

Once to this stage we’ll move on to a new page.

Getting Started with PHP part 3

Now we’re ready to start putting some of this stuff to use!

OK now we have installed IIS (the server) and the PHP script engine to help IIS to run our PHP files. We need somewhere to put them. This is where IIS comes in. We need to create a virtual directory, if like me your inital thought was whats all that mean then? I’ll try and explain

Right well the easiest way to tell you about these is by example. Us Brits see virtual directories everytime we watch the BBC. They are always advertising their website e.g. or Now the bit on the end is the virtual directory, the /sport or /nature the is the domain name of the BBC. When we type in the browser will check with IIS and IIS will do 1 of 3 things:

redirect you to another URL (website)
redirect you to another directory somewhere on a network
redirect you to a directory on your local computer
So, what does this mean…?
It means that when you type in IIS looks at where that location points to (out of the above list) and takes the appropriate action, so if you have a directory or folder full of HTML files and you point IIS to look at that folder, when someone enters the name of your virtual directory IIS will go to that directory on your computer and pull out the file named either “index.htm” or “default.htm” or which ever file you specify.

So an example to make this a little clearer, you have a folder c:\yourvirtaldirectory and a virtual directory called /yourvirtualdirectory. in that local folder you have an index.htm page. When you type in http://localhost/yourvirtualdirectory (providing it has been set up correctly which we will do next), IIS should pull out your default document

e.g. http://yourcomputername/your-virtual-directoryname will point to c:\yourvirtualdirectory and vice versa.
This should become clearer once you’ve read getting started with PHP part 4.

Getting Started with PHP # 2

Ok, to get on with some PHP we need to download the engine, what is that…? well…the php engine is something that you install to your machine that allows IIS to process PHP pages. When initially installed IIS will only run certain types of pages, these include .htm, .html (these are both the same) and .asp(Active Server Pages).
Active Server Pages are a Microsoft technology so naturally Microsoft made IIS able to process .asp files. HTML is what is called an open standard and has been around for ages, and is an industry standard language(default if you like) for writing webpages so it would not be very practical if IIS web server did not run HTML pages(see for more info)

So in order to make IIS run PHP pages you need what is called a plugin, or more precisley a script engine.

So how/where do you get the PHP script engine?
Simple, navigate (open a browser and go to…) to and you can download it from there. I am not going to go into much detail about downloading the php engine as i am assuming you all know how to download files and use the web to a basic level. I will recommend one thing though. Download a Windows Binary (i am assuming you are using windows) and get an installer they are clearly labelled on the PHP site, also i recommend version 4.3 and above…at the time of writing they were the most stable versions.

After your files Downoads
Once the file has downloaded, find it on your hard drive and open it, it should(if you got the windows installer) take you through an installation wizard, all being well. Now its pretty straight forward, keep pressing next. You will get a couple of options about a mail server, leave those as they are, and then something about the IIS version. Now, to be on the safe side. set it to version 4.

If everything went well then you can move on…to getting started with PHP part 3!

Getting Started with PHP

So, Getting started with PHP huh? now you may have noticed that this site, was all made from .htm files. You are right in thinking that I made it entirley in HTML. The reason for this is simple, because I didn’t know any back then! Plus there are also some things we need to do before we start coding PHP.

The Following set of tutorials details how to set up your PC to start writing PHP scripts!

Right if you are the average windows user you will probably be supprised, like i was, to know that you can turn your PC into a web server for your own personal use, and if your on a network, others will be able to see your pages once you’ve made some.

So how do we do this?

Setting up Internet Information Services on Windows XP
OK, to start with, from your desktop, click the start button, settings, control panel.
In the control panel, select “Add/Remove Programs” and Click Windows Setup or add/remove windows Components

Add/Remove Components in the Control Panel - This is to make windows take a look at what<br /><br />
snap-ins are currently installed on your machine.
Add/Remove Components in the Control Panel – This is to make windows take a look at what snap-ins are currently installed on your machine.

A list of programs should appear, ones with ticks next to them are installed, ones without are missing.
Scroll down and put a tick next to Internet Information Services(IIS) and press next. Windows then does some jiggery pokery and installs IIS or the web server bit to me and you. Please note this is for XP pro users only and you will need your windows disk for this bit.

Boring Part 1 over – Lets get the things needed to do some PHP with!

You’ll need to check out Getting Started with PHP pt 2 for that!