Reputation: 13
I'm creating a HTML user form that when submitted, it gets posted to a PHP page to print on screen what the user typed in the form fields.
My problem: With my existing PHP script, it writes a blank line where no data is in the HTML field. I want the results to skip the whole line if a HTML field was left blank by the user. Is this possible?
I have a pretty standard form. I use $_POST. I currently use this:
<?php if (!empty($_POST['fax'])) {
echo "Fax "; echo $_POST['fax'];} ?><br>
But it leaves a blank line. The result should be a complete email signature. It looks something like this
Lisa Simpson
CEO
Board of Directors
Tel.: 213123
Fax: 123123
Mobile: 123123
I want that if the user doesn't have a faxnumber and leaves it blank, it should look like this:
Lisa Simpson
CEO
Board of Directors
Tel.: 213123
Mobile: 123123
Thanks for any help in advance.
Upvotes: 1
Views: 3006
Reputation: 1531
Check if the length of the $_POST['fax'] variable is greater than 0, then you will know for sure if it has been set.
if (strlen($_POST['fax']) > 0) {
echo "Fax: " . $_POST['fax'];
}
Upvotes: 1
Reputation: 575
elegant way
// binding data
$template = [
'key' => ['text', null]
'kye2' => ['text', null],
];
foreach ($_POST as $key => $value)
foreach ($template as $template_key => $template_value)
if ($key == $template_key)
$result[] = $template_value['1'] = $value;
//display data
foreach ($result as $field) echo $field['0'], ': ', $field['0'], '<br>';
Upvotes: 0
Reputation: 54
Try This :-
<?php if (!empty($_POST['fax'])) {
echo "<br> Fax "; echo $_POST['fax'];} ?>
Upvotes: 0
Reputation: 1
I't seems that you sends the
eahn time, causing the blank line.
Yo can correct it using:
<?php if (!empty($_POST['fax'])) {
echo "Fax "; echo $_POST['fax']."<br>";
} ?>
Upvotes: 0
Reputation: 72299
The problem is <br>
tag id outside of the condition, So either value is there or not <br>
will going to execute and give empty line. Try like this:-
<?php if (!empty($_POST['fax'])) {
echo "Fax "; echo $_POST['fax'].'<br>';} ?>
Note:- You need to do it for all your !empty
conditions.thanks. The above code gives new line only if values are there otherwise nothing will be output.
Upvotes: 0