Tag Archives: php tutorials

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.

Making a PHP File

Right let’s have a crack at some php then, all you need to make a php file is Notepad or any text editor for that matter.

So, let us open up notepad… as I make up code etc it will be added to the live version of this site. So if you are visiting when I have got good at PHP then it will please you to know that this site was once a bunch of html pages.

Enough of this gay banter, let us get started…

The first characters you should include in your php file is a <? opening tag and an ?> closing tag. So any code between these markers will be executed as php code. as long as your file extension is .php

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. bbc.co.uk/nature or bbc.co.uk/sport. Now the bit on the end is the virtual directory, the /sport or /nature the bbc.co.uk is the domain name of the BBC. When we type in bbc.co.uk/sport 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 bbc.co.uk/sport 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 w3c.org 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 www.php.net 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!