Script 2.5 Form Validation(2)

Example of using !empty() to check if a form value is submitted from the $_POST Superglobal.

Output
Enter your information in the form below:

Male Female

Source
<?php
#	Script	2.5	-	form processing using Post Superglobal Array
//check if form has been submitted
$sent = (isset($_POST['submit']))?$_POST['submit']:null;
if($sent) { 
	//	Print the submitted	information:
	if ( !empty($_POST['name']) && !empty($_POST['comments']) && !empty($_POST['email']) ) {
		echo "<p>Thank you, <b>{$_POST['name']}</b>, for the following comments:<br />
		<tt>{$_POST['comments']}</tt></p>
		<p>We will reply to you at <i>{$_POST['email']}</i>.</p>\n";
	} else {	//	Missing	form value.
		echo '<p>Please go back and fill out the form again.</p>';
		echo '<input type="button" value="Back" onclick="window.history.back();">';
	}
} else { ?>
	<!-- Script 2.1 - the form -->
	<form action='#' method="post">

		<fieldset><legend>Enter your information in the form below:</legend>
		
		<p><label>Name: <input type="text" name="name" size="20" maxlength="40" /></label></p>
		
		<p><label>Email Address: <input type="text" name="email" size="40" maxlength="60" /></label></p>
		
		<p><label for="gender">Gender: </label><input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" /> Female</p>
		
		<p><label>Age:
		<select name="age">
			<option value="0-29">Under 30</option>
			<option value="30-60">Between 30 and 60</option>
			<option value="60+">Over 60</option>
		</select></label></p>
		
		<p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p>
		
		</fieldset>
		
		<p align="center"><input type="submit" name="submit" value="Submit My Information" /></p>

	</form>
<?php } ?>