Access Form Data from PHP

by Vincy. Last modified on March 23rd, 2023.

HTML forms are used to allow users to enter their input to the application. The form element can contain multiple fields like input box, select box, text area, checkbox, radio button and more.

Using these fields, the user can enter the data and submit to the server-side PHP file. The PHP file name to be called on the submit event will be specified by using the for action attribute.

The form field values are added to the PHP global array based on the request type specified in the form by using the method attribute. If the form method is GET then the form fields values are stored into the $_GET array.

If it is POST, then the values are posted with the $_POST array. In the PHP file, the $_GET or $_POST request array is used to access and process the user input submitted via the form.

In this tutorial, I have an example with an HTML form containing the various type of input fields. On submitting this form, the field values are accessed from a PHP code and populated the submitted values in the form.

View Demo

The following screenshot shows the HTML form output after submitting the user data. It populates the submitted values by using PHP code.

form-submit-output

HTML Form and Prepopulated Values

The following HTML code contains a form with various type of input fields like text input, text area, select box, radio, checkbox. After entering data into these fields, the user can submit it to the PHP file.

Since the action attribute is empty, the form submit will process the same page and execute the PHP code to access the form data.

In this example, we use PHP to check the form fields values are not empty to populate them in the form. For populated the selected items in the input combo, we check each item, if it is selected by the user.

For example, if the user checked multiple items from the checkbox group, we check each checkbox item if it is in the $_POST array.

<form name="frmTutorial" method="post" action="">
	<table border="0" width="500" align="center" class="demo-table">
		<tr>
			<td>Title<br /> <input type="text" class="demoInputBox" name="title"
				value="<?php echo $title; ?>"></td>
		</tr>
		<tr>
			<td>Description<br /> <textarea class="demoInputBox"
					name="description" cols="10"><?php echo $description; ?></textarea></td>
		</tr>
		<tr>
			<td>Category<br /> <select name="category" class="demoInputBox">
					<option value="Single">Single</option>
					<option value="Group">Group</option>
					<option value="Team">Team</option>
					<option value="All">All</option>
			</select>
			</td>
		</tr>
		<tr>
			<td>Tags<br /> <input type="checkbox" name="tags[]" value="PHP"
				<?php if(!empty($_POST['tags']) && in_array("PHP",$_POST['tags'])) { ?>
				checked <?php  } ?>> PHP <input type="checkbox" name="tags[]"
				value="HTML"
				<?php if(!empty($_POST['tags']) && in_array("HTML",$_POST['tags'])) { ?>
				checked <?php  } ?>> HTML <input type="checkbox" name="tags[]"
				value="FORM"
				<?php if(!empty($_POST['tags']) && in_array("FORM",$_POST['tags'])) { ?>
				checked <?php  } ?>> FORM
			</td>
		</tr>
		<tr>
			<td>Active<br /> <input type="radio" name="status" value="1"
				<?php if(!empty($_POST['status'])) { ?> checked <?php  } ?>> Yes <input
				type="radio" name="status" value="0"
				<?php if(empty($_POST['status'])) { ?> checked <?php  } ?>> No
			</td>
		</tr>
		<tr>
			<td><input type="submit" name="submit-form" value="Submit"
				class="btnSubmit"></td>
		</tr>
	</table>
</form>

In this example, the PHP code gets values posted via the form.

It filters the posted data of the title and the description fields using PHP htmlspecialchars().

This function converts the special characters into an equivalent HTML entity.

This function will retain the exact data the user enters, even if it has a special meaning in HTML. This function will prevent the execution of HTML script if posted as part of the form data.

$title = "";
if (! empty($_POST["title"])) {
    $title = htmlspecialchars($_POST["title"]);
}
$description = "";
if (! empty($_POST["description"])) {
    $description = htmlspecialchars($_POST["description"]);
}
?>

View DemoDownload

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “Access Form Data from PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page