codescracker


php

PHP Read Requested Data



« Previous Tutorial Next Tutorial »


You can read the requested data using PHP like, when user enters their information into the form and wants to review the entered details, then you have to first read that data and prints back to the browser.

How to Read Requested Data using PHP

Here is an example shows how any requested data can be read using PHP.

<?php
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		// reading the requested data
		// and storing the value to separate 
		// variables
		if(isset($_POST['data1']))
		{
			$data1 = $_POST['data1'];
		}
		if(isset($_POST['data1']))
		{
			$data2 = $_POST['data2'];
		}
		if(isset($_POST['data3']))
		{
			$data3 = $_POST['data3'];
		}
		if(isset($_POST['data4']))
		{
			$data4 = $_POST['data4'];
		}
	}
?>
<html>
<head>
	<title>PHP Reading Requested Data Example</title>
</head>
<body>
<?php 
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		echo "Reading first data...<br/>";
		if(!empty($data1))
		{
			echo "The first data is: ".$data1;
		}
		else 
		{
			echo "Sorry, first data is empty.";
		}
		echo "<hr/>";
		echo "Reading second data...<br/>";
		if(!empty($data2))
		{
			echo "The second data is: ".$data2;
		}
		else 
		{
			echo "Sorry, second data is empty.";
		}
		echo "<hr/>";
		echo "Reading third data...<br/>";
		if(!empty($data3))
		{
			echo "The third data is: ".$data3;
		}
		else 
		{
			echo "Sorry, third data is empty.";
		}
		echo "<hr/>";
		echo "Reading fourth data...<br/>";
		if(!empty($data4))
		{
			echo "The fourth data is: ".$data4;
		}
		else 
		{
			echo "Sorry, fourth data is empty.";
		}
		exit();
	}
?>
<form method="POST">
	Enter Data 1: <input type="text" name="data1"><br/>
	Enter Data 2: <input type="text" name="data2"><br/>
	Enter Data 3: <input type="text" name="data3"><br/>
	Enter Data 4: <input type="text" name="data4"><br/>
	<input type="submit" Value="Submit">
</form>
</body>
</html>

Here is the sample output (initial output) produced by the above reading requested data example code in PHP.

php reading requested data

Now enter the information into all the four input boxes like this:

read request data php

Now click on Submit button and watch the output, the output will looks like:

reading requested data example php

Again run the same code and this time fill only two box and left the rest two box as blank like this:

php reading data

Now click on Submit button, the output this time will looks like:

read data in php

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test