johnhewitt Posted January 2, 2009 Share Posted January 2, 2009 Hello everybody. I'm a new php freak from UK. I need help with processing a test form. The problem I am having is that the php process does not seem to recognise an empty <textarea> name="comments" section. If i leave the textarea blank no error message comes up, yet it does with all the other fields. I hope this is ok for a first post! The code is as follows: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Form Proccesing</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <style type="text/css">@import url(form.css);</style> </head> <body> <?php $first_name=$_POST['first_name']; $second_name=$_POST['second_name']; $email=$_POST['email']; $comments=$_POST['comments']; //first name checker if (!empty ($_POST['first_name'])) { $first_name=$_POST['first_name']; } else { $first_name=NULL; echo '<p class="error">You need to enter a first name</p>'; } //second name checker if (!empty ($_POST['second_name'])) { $second_name=$_POST['second_name']; } else { $second_name=NULL; echo '<p class="error">You need to enter a second name</p>'; } //email checker if (!empty ($_POST['email'])) { $email=$_POST['email']; } else { $email=NULL; echo '<p class="error">You need to enter a valid email.</p>'; } //comment checker if (!empty($_POST['comments'])) { $comments=$_POST['comments']; } else { $comments=NULL; echo '<p class="error">You need to enter a comment</p>'; } if ($first_name && $second_name && $email && $comments) { echo "<p>Thank you $first_name $second_name and thank you for the following comments: \"$comments\". We will reply at $email</p>"; } ?> </body> </html> The HTML for the form itself is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Form</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <style type="text/css">@import url(form.css);</style> </head> <body> <form action="form_processor.php" method="post" > <fieldset> <legend>Please fill in your details below</legend> <p>First Name: <input type="text" name="first_name" size="40" maxlength="60" /> </p> <p>Second Name: <input type="text" name="second_name" size="40" maxlength="60" /> </p> <p>Please select: <input type="radio" name="gender" value="M" />Male <input type="radio" name="gender" value="F" />Female </p> <p>Please enter your email address: <input type="text" name="email" size="40" maxlength="60"/> </p> <p>Please select your age <select name="age"> <option value="20-30">20-30</option> <option value="40-50">40-50</option> <option value="60+">60+</option> </select> </p> <p> Choose a password: <input type="password" name="password" size="40" maxlength="60"/> </p> <p> Please leave a comment: <textarea name="comments" rows="5" cols="40"> </textarea> </p> <p><input type="submit" value="Go!" /></p> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/ Share on other sites More sharing options...
ratcateme Posted January 2, 2009 Share Posted January 2, 2009 try using trim() like this //comment checker if (!empty($_POST['comments'])) { $comments=trim($_POST['comments']); } else { $comments=NULL; echo '<p class="error">You need to enter a comment</p>'; } you also might want to consider using it on your other fields it will take out spaces \r \n and tabs that are at the start ir end of a string so someone might enter a space for a first name you script will allow that with trim it will return an empty string and be disallowed Scott. Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728377 Share on other sites More sharing options...
johnhewitt Posted January 2, 2009 Author Share Posted January 2, 2009 Hello and thanks for the reply. It didn;t work unfortunately. If no comments are entered the final echo doesn't show but the error message for no text entered still doesn't come up. It's strange because all the other warnings show when fields are blank and the code is identical. This is really puzzling me! ??? Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728438 Share on other sites More sharing options...
Brian W Posted January 2, 2009 Share Posted January 2, 2009 Please use the code tags around your code [ code ]code here[ /code ] //without spaces inside the [] looks like this code here if (trim($_POST['comments']) > "") { $comments=trim($_POST['comments']); } else { $comments=NULL; echo '<p class="error">You need to enter a comment</p>'; } Think that'll do what you are looking for Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728439 Share on other sites More sharing options...
johnhewitt Posted January 2, 2009 Author Share Posted January 2, 2009 wow; that worked perfectly thank you very much. Why was the textarea comment code effected even though it had the same code as the other variables; which worked fine? Thank you for the solution though! Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728445 Share on other sites More sharing options...
Brian W Posted January 2, 2009 Share Posted January 2, 2009 Well, I'm thinking that text inputs treat empties as NULL or EMPTY while texareas treat empties as "" (no text but still NULL or EMPTY, not the same). Also, <textarea name="comments" rows="5" cols="40"> </textarea> is the same as <textarea name="comments" rows="5" cols="40"> </textarea> which is not empty, it has a bunch of spaces. So that is why I also included trim() around the variable which got rid of the extra white space. Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728455 Share on other sites More sharing options...
johnhewitt Posted January 2, 2009 Author Share Posted January 2, 2009 That's very cool. I tested space bar in the other fields and allowed them so I added the trim() to all the other variables too and it all runs perfectly. Thank you so much. Problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/139246-solved-form-processing/#findComment-728461 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.