To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

PHP tutorialForms in PHP


A form is almost exactly as it sounds - information is completed by a user into fields and it is then processed by PHP.

There is a another option of doing this known as AJAX, but it will be covered in the JavaScript tutorial on this website.

PHP pages can be used as simply using code and a redirect without the need for any HTML or output.

Below is a small form which has no submit method.

This is a form.

You can select one of the following options and fill in certain parts of it.



Experience with PHP:


Text can be included in a form.

The form above has no action therefore it will not do anything.

The code for that form is shown below:

<!--This form uses the Epic Form class on this website.-->
<form class="epic">
	<p>This is a form.</p>
		You can select one of the following
		options and fill in certain parts of it.
	<p>Name: <input name="name"  type="text" style="width:200px;"></p>
	<p>Address: <input name="address" type="text" style="width:200px;"></p>
	<p>Experience with PHP:</p>
	<input checked name="experience" type="radio" value="none">None<br>
	<input name="experience" type="radio" value="lots">Lots<br>
	<p>Text can be included in a form.</p>
	<p><input name="Submit1" type="submit" value="Submit"></p>

The input element is used to collect an input whilst the type attribute specifies what type of input will be provided, for instance, checkbox, radio button etc.

<!--This will result in address being passed to the page sample.php-->
<form action="sample.php" style="border:thin;">
	<input name="address" type="text" style="width:100%;">
	<input name="Submit1" type="submit" value="Submit">

This means that when the user submit the form, the contents of the form input values will be passed to the page sample.php.

Receiving the form at the other end

One of the fundamentals of PHP and forms is extracting the information which has been given to it. This can achieved using the $_POST['inputName'] method where the inputName is replaced with the name of the form input that we want the value of. This example will look at using both the $_POST['inputName'] variable and the echo command.

So in the previous example, in results to the form shown previously, the following code will the name that has been submitted:

	echo $_POST['name'];

This code is taking the form value from the input called "name". In this sample it is being put inside a paragraph element. Alternatively to the $_POST['inputName'] method, there is a $_GET['inputName'] method.

The $_GET variable (it is actually an associative array variable, but this will be discussed later) can be used to obtain URL variables such as those that follow a question mark in a query string in a URL, for example: page.php?p=1277.