Jump to content
Larry Ullman's Book Forums

Recommended Posts

I can't get any of the scripts to work unless I cut and paste them after downloading them. It must be some little thing I'm missing or a typo but I've gone over the scripts over and over comparing what I typed into my code editor to what I cut and pasted and cannot find any discrepencies. This is one example of one that is not working:

 

<!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" xml:lang="en" lang="en">

<head>

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

<title>Form Feedback</title>

<style type="text/css" title="text/css" media="all">

.error {

font-weight: bold;

color: #C00;

}

</style>

</head>

<body>

<?php # Script 2.4 - handle_form2.php #3

 

// Validate the name:

if (!empty($_REQUEST['name'])) {

$name = $_REQUEST['name'];

} else {

$name = NULL;

echo '<p class="error">You forgot to enter your name!</p>';

}

 

// Validate the email:

if (!empty($_REQUEST['email'])) {

$email = $_REQUEST['email'];

} else {

$email= NULL;

echo '<p class="error">You forgot to enter your email address!</p>';

}

 

// Validate the comments:

if (!empty($_REQUEST['comments'])) {

$comments= $_REQUEST['comments'];

} else {

$comments = NULL;

echo '<p class="error">You forgot to enter your comments!</p>';

}

 

// Validate the gender:

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

 

$gender = $_REQUEST['gender];

 

if ($gender == 'M') {

$greeting ='<p><b>Good day, Sir!</b></p>';

} elseif ($gender== 'F') {

$greeting = '<p><b>Good day, Madam!</b></p>';

} else { //Unacceptable value.

$gender=NULL;

echo '<p class="error">Gender should be either "M" or "F"!</p>';

}

 

} else { //$_REQUEST['gender'] is not set.

$gender = NULL;

echo '<p class="error">You forgot to select your gender!</p>';

}

// If everything is OK, print the message:

if ($name && $email && $gender && $comments) {

 

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";

 

echo $greeting;

 

} else { // Missing form value.

echo '<p class="error">Please go back and fill out the form again.</p>';

}

 

?>

</body>

</html>

 

Am I missing something?

Link to comment
Share on other sites

When you say not working, what do you mean? What errors (if any do you get?)

 

One problem is that you haven't matched some quotes up

 


<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Form Feedback</title>
<style type="text/css" title="text/css" media="all">
.error {
font-weight: bold;
color: #C00;
}
</style>
</head>
<body>
<?php # Script 2.4 - handle_form2.php #3

// Validate the name:
if (!empty($_REQUEST['name'])) {
$name = $_REQUEST['name'];
} else {
$name = NULL;
echo '<p class="error">You forgot to enter your name!</p>';
}

// Validate the email:
if (!empty($_REQUEST['email'])) {
$email = $_REQUEST['email'];
} else {
$email= NULL;
echo '<p class="error">You forgot to enter your email address!</p>';
}

// Validate the comments:
if (!empty($_REQUEST['comments'])) {
$comments= $_REQUEST['comments'];
} else {
$comments = NULL;
echo '<p class="error">You forgot to enter your comments!</p>';
}

// Validate the gender:
if (isset($_REQUEST['gender'])) {

$gender = $_REQUEST['gender']; // missing closing '

if ($gender == 'M') {
$greeting ='<p><b>Good day, Sir!</b></p>';
} elseif ($gender== 'F') {
$greeting = '<p><b>Good day, Madam!</b></p>';
} else { //Unacceptable value.
$gender=NULL;
echo '<p class="error">Gender should be either "M" or "F"!</p>';
}

} else { //$_REQUEST['gender'] is not set.
$gender = NULL;
echo '<p class="error">You forgot to select your gender!</p>';
}
// If everything is OK, print the message:
if ($name && $email && $gender && $comments) {

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";

echo $greeting;

} else { // Missing form value.
echo '<p class="error">Please go back and fill out the form again.</p>';
}

?>
</body>
</html>

 

The code you pasted now validated syntactically in my editor. What editor are you using, a good one should bring these oversights to your attention.

Link to comment
Share on other sites

I tried turning display errors on in my php.ini file and it didn't work. When I get the white screen and I try to view source code that is blank also. When I cut and pasted your revision of my code above it worked. So maybe it was just the missing quotes. I'm going to try to hand write the code again and see if it works.

Link to comment
Share on other sites

  • 2 years later...
 Share

×
×
  • Create New...