Jump to content

How to skip a blank field in a form when posted


bbw82

Recommended Posts

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 />

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.