Use the $_GET[] and $_POST[] arrays to read data sent via web form.

The $_GET[] array contains data sent using the GET method. The $_POST[] array contains data sent using the POST method. Other than that, $_GET[] and $_POST[] work the same.

Typically, a web form element has a unique name attribute. The input name becomes the key that we use in PHP to read the user’s data.

Example, the user’s web form has a field like this:

<input type="text" name="message" />

Assume that the input is inside a form tag, and the form tag’s action property is set to some PHP script. To make PHP output the user’s message, do this:

$msg = $_POST['message'];
print htmlspecialchars($msg);

This code will print a message from the user. The code expects the user’s web form to have a field named ‘message’. The code also expects the user’s form to have it’s method property set to POST.

We use the htmlspecialchars() function when displaying data directly to the web like this, for security reasons.

Sample web form

<form id="form1" name="form1" method="get" action="show-form-data.php">
    <label for="email"></label>
    <input type="text" name="email" id="email" /> 
<p><input type="text" name="message" /> Message</p>
    <input type="submit" name="button" id="button" value="Submit" />

See also