Jump to content

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


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"];



echo $_POST["Middle"];



echo $_POST["Last"];

Link to comment
Share on other sites

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




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

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.

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.