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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.