im_a_n00b Posted April 23, 2008 Share Posted April 23, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/ Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 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. =) Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524942 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524945 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524950 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks for the help, the books I'm using are PHP and MySQL 5 by W. Jason Gilmore and Web 2.0 applications with PHP by Quentin Zervaas. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524966 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 I'm still having a problem with the gender validation. It's working but it's not displaying the message. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524988 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 Grr. Add this to the end of the page (in PHP tags) and copy and paste what it outputs: print_r ($_POST); Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-524993 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 From where I'm sitting your skills are sick. If you tell me that book helped you get that way, I'm ordering it today. Array ( [name] => => [age] => 0-29 [comments] => [submit] => Submit My Information ) Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525008 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525016 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525027 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525032 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 Oh, yeah, gender can't have a value other than M or F because then it sets it to NULL which means that the variable is set to nothing. You need to enter M or F. I could code up something that would force M or F if you want. >_> Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525037 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 I a fairly capable C++ programmer so I think I could code something better that this up. I just wanted to try and fix the code the way it was written. What I don't understand is why there are two else statements for gender. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525061 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 The first else statement is for the if elseif else conditional, and the second one is on the outside of that, and is for the if (isset($_POST['gender'])) conditional. So the code works when you enter an appropriate gender, correct? Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525065 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525079 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 Correction the nested conditional is fine but the else statements are weird as they are both the same. I really want to understand this before I move on in the book. Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525093 Share on other sites More sharing options...
im_a_n00b Posted April 23, 2008 Author Share Posted April 23, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/102532-solved-form-validation-help/#findComment-525116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.