Monthly Archives: January 2018

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.