Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am having a problem with the Gender variable on a form. When I test the form in the browser the message works fine ie "Good Day, Sir", However when I change the radio button to select Female I get the message "You have not selected your gender".

 

I have checked and double checked the code and it's exactly as it is written in the book. I have pasted the HTML form code and the Handle Form.php code. Could someone take a look and see if I'm missing something please.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Simple HTML Form</title>

</head>

<body>

<!--Script 2.1 - Form HTML -->

<form action="handle_form.php" method="post">

<fieldset><legend>Enter your information in the form below:</legend>

<p><b>Name:</b> <input type="text" name="name" size="20" maxlengh="40" /></p>

<p><b>Email Address:</b> <input type="text" name="email" size="20" maxlengh="40" /></p>

<p><b>Gender:</b> <input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" />Female</p>

<p><b>Age:</b>

<select name="age">

<option value="0-29">under30</option>

<option value="30-60">Between 30 and 60 </option>

<option value="60+">Over 60</option>

</select></p>

<p><b>Comments:</b> <textarea name="comments" rows="3" cols="40"></textarea></p>

</fieldset>

<div align="center"><input type="submit" name="submit" value="Submit my Information" /></div>

</form>

</body>

</html>

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Form Feedback</title>

</head>

<body>

<?php # Script 2.3 - handle_form.php #2

 

// Create a shorthand for the form data:

$name = $_REQUEST['name'];

$email = $_REQUEST['email'];

$comments = $_REQUEST['comments'];

 

// create the $gender variable:

if (isset($_REQUEST['gender'])) {

$gender = $_REQUEST['gender'];

} else {

$gender = NULL;

}

// 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>';

}

?>

</body>

</html>

Link to comment
Share on other sites

 Share

×
×
  • Create New...