Script 2.3 Conditional Output

Example of using isset to check if a form value is submitted and conditionally output a message.

Output
Enter your information in the form below:

Male Female

Source
<?php # Script 2.3 - form processing with conditional 

// Create a shorthand for the form data:
$name = (isset($_REQUEST['name']))?$_REQUEST['name']:null;
$email = (isset($_REQUEST['email']))?$_REQUEST['email']:null;
$comments = (isset($_REQUEST['comments']))?$_REQUEST['comments']:null;
$sent = (isset($_REQUEST['submit']))?$_REQUEST['submit']:null;

// Create the $gender variable:
if (isset($_REQUEST['gender'])) {
	$gender = $_REQUEST['gender'];
} else {
	$gender = NULL;
}

if($sent){
	// Print the submitted information:
	echo "<p>Thank you, <b>$name</b>, for the following comments:<br />
	<tt>$comments</tt></p>
	<p>We will reply to you at <i>$email</i>.</p>\n";

	// Print a message based upon the gender value:
	if ($gender == 'M') {
		echo '<p><b>Good day, Sir!</b></p>';
	} elseif ($gender == 'F') {
		echo '<p><b>Good day, Madam!</b></p>';
	} else { // No gender selected.
		echo '<p><b>You forgot to enter your gender!</b></p>';
	}
} else { ?>
	<!-- Script 2.1 - the form -->
	<form method="post" action="#">

		<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><input type="submit" name="submit" value="Submit My Information" /></p>

	</form>
<?php } ?>