Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 3.4Forms in PHP

Forms

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.

Name:

Address:

Experience with PHP:

None
Lots

Are you happy to submit your data to third-parties?

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

The code for that form is shown below:

HTML
<!--This form uses the Epic Form class on this website.-->
<form class="epic">
	<p>This is a form.</p>
	<p>
		You can select one of the following
		options and fill in certain parts of it.
	</p>
	<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>
</form>
								

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.

HTML
<!--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">
</form>
								

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 be 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:

HTML + PHP
<p>
<?php
	echo $_POST["name"];
?>
</p>
								

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.

Working with checkboxes

When processing a form on the server-side, it is often the case you want to work with check boxes (input type="checkbox") and radio buttons (input type="radiobutton"). When working with checkboxes things are a bit different to standard inputs.

This is because if a checkbox is not ticked on the front-end, the value is not actually submitted at all. Trying to access the value is therefore not possible.

To fix this, the isset function is used:

HTML + PHP
<p>
<?php
  if (isset($_POST["submit_your_data"])) {
	   echo "You have agreed to allow us to submit your data to third-parties!";
  } else {
    echo "Why didn't you agree to allow us to submit your data to third-parties?!";
  }
?>
</p>
								

If the isset is not used to verify that the key actually exists, the PHP interpreter will likely throw an error (unless errors are switched off) and the value retrieved from the $_POST associative array variable will be null.

Feedback 👍
Comments are sent via email to me.