Forms, GET and POST

 

A form consists of the form definition, identifying what it should do (action) and how it should do it (method).

 

<form action="submittedPage.php" method="POST">

 

Will send any collected data to the specified page, in this case: submittedPage.php, using the POST method.

 

$_GET and $_POST are the two METHODS used to retrieve user entered information input to a form upon Submit.

 

  • $_GET insecure, data can be seen in the URL
  • $_POST data cannot be seen in the URL

 

<html>
<body>
	<form action="submittedPage.php" method="POST">
		Name: <input type="text" name="formName"><br>
		Age: <input type="text" name="formAge"><br>
		Location: <input type="text" name="formLoc">
		<input type="submit">
	</form>
</body>
</html>

The above HTML code for a simple form provides three text boxes and a submit button.

 

Each element within the form has a specific name. This name is used for the collected data:

 

Name: <input type="text" name="formName"><br>
Age: <input type="text" name="formAge"><br>
Location: <input type="text" name="formLoc">

 

Once the user has entered their data and clicked upon submit, the "submittedPage.php" page will be loaded.

 

We then use the $_POST super global variable to present the collected information.

 

The element names of the form fields will automatically be the keys in the $_POST array.

 

Notice that the element name is specified within "double quotes" within [square brackets]

 

<?php
	echo "The information submitted was:<br><br>" ;
	echo $_POST["formName"] . "<br>";
	echo $_POST["formAge"] . "<br>";
	echo $_POST["formLocation"] . "<br>";
?>

Displays:

The information submitted was:

 

Julia
21
Camden

 

*note, the URL will simply display the submittedPage.php

 

 

Alternatively, we can use the GET method.

 

<html>
<body>
	<form action="submittedPage.php" method="GET">
		Name: <input type="text" name="formName"><br>
		Age: <input type="text" name="formAge"><br>
		Location: <input type="text" name="formLoc">
		<input type="submit">
	</form>
</body>
</html>

Now we collect the information using the $_GET super global variable.

 

The element names of the form fields will automatically be the keys in the $_GET array

 

<?php
	echo "The information submitted was:<br><br>" ;
	echo $_GET["formName"] . "<br>";
	echo $_GET["formAge"] . "<br>";
	echo $_GET["formLocation"] . "<br>";
?>

Displays:

The information submitted was:

 

Jimmy
25
Camden

 

However, this time the URL displays all of the collected data !

 

.../submittedPage.php?formName=Jimmy&formAge=25&formLocation=Camden

 

Notice the URL contains the name of the page specified in the action, followed by a ? and each of the element names and their submitted data, separated by an &

Leave a Reply