Jump to content

Recommended Posts

I'm working on some practice programs from a book and the scripts are not working properly. Naturally I though this was my fault and checked the files from the companies website. The same problem existed here. I've attached both files I'm working with and help would be greatly appreciated. The script is suppose to validate a form. If everything isn't filled out on the form it should display warning messages and nothing else until the entire form is filled out. As of now it only recognizes if gender isn't filled out and displays the rest of the message. I hope I was clear enough.Please help!!!

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/
Share on other sites

PHP and MySQL for Dynamic Web Sites by Larry Ullman?  AMAZING book.  Jumpstarted my PHP career and made me 50x better.  And then years of practice helped, lol.  Stick with this book.  I'll run the files on my server and see what's wrong. =)

PHP and MySQL for Dynamic Web Sites by Larry Ullman?  AMAZING book.  Jumpstarted my PHP career and made me 50x better.  And then years of practice helped, lol.  Stick with this book.  I'll run the files on my server and see what's wrong. =)

I'll pick up that book. I've purchased so many. One more can't hurt.

Oh, you don't have that book?  What book do you have?  Because the coding style is the exact same, and that script is the same as when I looked at my book.  You might have PHP6 and MySQL5, which is the latest version.

 

Btw, here's the new code. =)

<!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=iso-8859-1" />


<title>Form Feedback</title>


</head>


<body>


<?php # handle_form.php 





// Validate the name and combat Magic Quotes, if necessary.


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


$name = stripslashes($_POST['name']);


} else {


$name = NULL;


echo '<p><fontcolor="red">You forgot to enter your name!</font></p>';
}





// Validate the email and combat Magic Quotes, if necessary.


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


$email = $_POST['email'];


} else {


	$email = NULL;


echo '<p><font color="red">You forgot to enter your email address!</font></p>';


}





// Validate the comments and combat Magic Quotes, if necessary.


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


$comments = stripslashes($_POST['comments']);


} else {


$comments = NULL;


echo '<p><font color="red">You forgot to enter your comments!</font></p>';


}





//Validate gender.


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


$gender = $_POST['gender'];





if ($gender == 'M') {


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


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


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


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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}

}



// If everything is okay, 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 $message; // From the $gender conditional.





} else { // One form element was not filled out properly.


echo '<p><font color="red">Please go back and fill out the form again.</font></p>';


}





?>


</body>


</html>

 

I changed it to use $_POST instead of $_REQUEST (something Larry introduces a little while after this script if I remember correctly) and I fixed the syntax errors.  Works like a charm.

This time, send data from the HTML page TO the php page and tell me what print_r outputs. =P

 

And years of practice really brought me to this point, but that book was amazing.  It showed me enough to really get me started with PHP and MySQL, although I'd been tinkering with it for a while before the book.

As far as I can tell $gender works as long as it has a value of M or F. If it's NULL it's not being recognized.

 

 

Array ( [name] => John Smith => jsmith@mycompany.com [gender] => M [age] => 0-29 [comments] => This is a test!! [submit] => Submit My Information )

K, thanks for the print_r.  Try this new code:

 

<!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=iso-8859-1" />


<title>Form Feedback</title>


</head>


<body>


<?php # handle_form.php 





// Validate the name and combat Magic Quotes, if necessary.


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


$name = stripslashes($_POST['name']);


} else {


$name = NULL;


echo '<p><fontcolor="red">You forgot to enter your name!</font></p>';
}





// Validate the email and combat Magic Quotes, if necessary.


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


$email = $_POST['email'];


} else {


	$email = NULL;


echo '<p><font color="red">You forgot to enter your email address!</font></p>';


}





// Validate the comments and combat Magic Quotes, if necessary.


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


$comments = stripslashes($_POST['comments']);


} else {


$comments = NULL;


echo '<p><font color="red">You forgot to enter your comments!</font></p>';


}





//Validate gender.


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


$gender = $_POST['gender'];





if ($gender == 'M') {


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


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


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


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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}


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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}
      


// If everything is okay, 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 $message; // From the $gender conditional.





} else { // One form element was not filled out properly.


echo '<p><font color="red">Please go back and fill out the form again.</font></p>';


}





?>


</body>


</html>

I'm going to have to play around with this if statement. While the program works It bothers me. The two else statement are identical but I guess one of them doesn't do anything. I don't think the if statement is properly nested. This is my problem with some books. All too often the code is broken.

 

if ($gender == 'M') {


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


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


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


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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}


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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}

Ok I've got it figured out now. I took out the first else statement and closed the outside if statement before the second if statement.(if that makes any sense). Thank you for all your help.

 

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


$gender = $_POST['gender'];





if ($gender == 'M') {


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


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


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





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


$gender = NULL;


echo '<p><font color="red">You forgot to select your gender!</font></p>';


}  

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.