Google News
logo
PHP Program to $_REQUEST - Used to collect data after submitting an HTML form
In the following example of PHP program that demonstrates the usage of `$_REQUEST` superglobal variable to collect data submitted through an HTML form :
Program :
<!DOCTYPE html>
<html>
<head>
	<title>PHP $_REQUEST Example</title>
</head>
<body>

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
	$name = $_REQUEST['name'];
	$email = $_REQUEST['email'];
	$message = $_REQUEST['message'];

	echo "<h2>Form Data:</h2>";
	echo "Name: $name <br>";
	echo "Email: $email <br>";
	echo "Message: $message <br>";
}
?>

<h2>Contact Form</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<label for="name">Name:</label>
	<input type="text" id="name" name="name"><br>

	<label for="email">Email:</label>
	<input type="email" id="email" name="email"><br>

	<label for="message">Message:</label>
	<textarea id="message" name="message"></textarea><br>

	<input type="submit" value="Submit">
</form>

</body>
</html>
In this program, we first check if the request method is `POST` using `$_SERVER['REQUEST_METHOD']` superglobal variable. If it is `POST`, we collect the form data using `$_REQUEST` superglobal variable and display it.

The HTML form contains `name`, `email`, and `message` fields that will be submitted to the same PHP script. The `action` attribute of the form is set to `` to submit the form to the same script.

When the user submits the form, the form data is collected using `$_REQUEST` superglobal variable and displayed.