bbw82 Posted December 1, 2011 Share Posted December 1, 2011 I have a basicc form that takes users info and when they submit it it show on a new web page in the proper order. I need to know how do I skip a blank field. This form takes a users First, Middle, and Last name but many times they leave the middle name blank so I would want to skip this field from showing up when the form is posted. Here is what I have for the basic php script ..Would like to know how to skip a field if it is left blank on the form. echo $_POST["First"], print "."; ?><?php echo $_POST["Middle"], print "."; ?><?php echo $_POST["Last"], print "."; ?><?php echo $_POST["Persona1"], print ".";<br /> Quote Link to comment Share on other sites More sharing options...
Spring Posted December 2, 2011 Share Posted December 2, 2011 use isset eample: if(isset($_POST["Middle"])){ echo $_POST["Middle"]; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 2, 2011 Share Posted December 2, 2011 use isset Um, let's not, since that won't work. A field that a users does not fill in a value for is still set - it just happens to have a value of an empty string. And, you should always use trim() on user input. Otherwise, a value of only spaces will appear as having a non-empty value. $first = trim($_POST['First']); $middle = trim($_POST['Middle']); $last = trim($_POST['Last']); $personal = trim($_POST['Persona1']); if(!empty($middle)) { $middle = $middle.'.'; } echo "{$first}.{$middle}{$last}.{$personal}.<br>\n"; Although you should really think about using htmlentities() on the values to prevent any values that might be interpreted as HTML code from screwing up the page. Quote Link to comment Share on other sites More sharing options...
bbw82 Posted December 2, 2011 Author Share Posted December 2, 2011 Thanks for the help the first reply worked Quote Link to comment Share on other sites More sharing options...
Spring Posted December 2, 2011 Share Posted December 2, 2011 Fair enough..but couldn't he just use empty Threw this together real quick and it worked fine <html> <form action = "" method = "POST"> <input type = "text" name = "name"> <input type = "submit"> </form> </html> <?php if(!empty($_POST['name'])){ echo "set"; } else { echo "not set"; } However, isset did not work as you mentioned above. Quote Link to comment Share on other sites More sharing options...
Spring Posted December 2, 2011 Share Posted December 2, 2011 Otherwise, a value of only spaces will appear as having a non-empty value. Oh wow, you're right! I never knew that! looks like I learned something new! Quote Link to comment 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.