Displaying form data in PHP

A useful piece of code I wrote to display the data sent from any type of html form, simply copy this code below into a php file and use that file as the action in the html form, When you submit the form it will display the values of the form in the next page, this can be a great when trying to figure out why something is not working correctly, say for example in a contact form.

<?php
	ob_start(); 		// Turn on output buffering
	session_start();	// Initialize Session Data

	// CALL THE FORM DATA FUNCTION
	formData(); 		

// CHECK WHAT WAS SENT FROM THE FORM
function formData(){

	$errorFound = false;
	echo '<h1>Data sent from the form</h1>';
	echo '<small>Data sent from: ' . getenv("HTTP_REFERER") . '</small>';
	foreach ($_POST as $key => $value) {
		echo '<ul>';
		if ($value){
			echo "<li>$key = $value" . "</li>";
		}else{
			echo "<li><b>$key</b> = <font color=\"red\">< No Data Sent ></font></li>";
			$errorFound = true;
		}
		echo '</ul>';
	}
	if ($errorFound){ // Message to display if there was an error
		echo '<h3>NOTE: Some of the variables contain no data!</h3>';
		echo '<b>Possible Reasons:</b><br />The user did not enter anything into the fields provided and you do not have some sort of validation setup such as <a href"http://docs.jquery.com/Plugins/Validation">jQuery</a> for those fields<br />';
		echo 'Check that the name tags in your form match what is being checked for in the process.php file.';
	}
	exit();
}
?>
  • Share/Bookmark

You can follow any responses to this entry through the RSS 2.0 feed.