Jump to content

How to add a "." if a particular field is filled out on a form


bbw82

Recommended Posts

I have a form where people will be putting in specific info such as their names and other info.  When the form is posted I need to add a "." between the fields on the output depending on if it is filled out or not if field is blank do not need a "."

 

Example if they put in their name Bob Marley on the form I need it to appear on the output as Bob.Marley.

 

Here is the code using currently which also excludes blank fields

 

echo $_POST["First"];

 

  if(isset($_POST["Middle"])){

echo $_POST["Middle"];

}

 

echo $_POST["Last"];

Hi.  I'd suggest changing your code to what Mj mentioned.

http://www.phpfreaks.com/forums/index.php?topic=349006.0

 

 

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.

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.