Packy
Packy

Reputation: 3573

PHP if statement to echo info from form

I have a form review page that works great calling:

echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>";
echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>";

The problem is that I have a ton of fields that don't get filled out. I want to put an "if" statement before the echo to check if it has data. if not, nothing gets displayed. I am very new to PHP, so I'm not sure if I can call something like:

if (length > 0)echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";

Thanks in advance for any help. Cheers.

Upvotes: 1

Views: 131

Answers (6)

ROMMEL
ROMMEL

Reputation: 524

echo (!empty($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>  {$_REQUEST['CustomerName']}</div>" : "";
echo (!empty($_REQUEST['CustomerEmail'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>" : "";
echo (!empty($_REQUEST['CustomerPhone'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>" :"";
echo (!empty($_REQUEST['CustomerAddress'])) ? "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>" : "";

Try that.

Upvotes: 1

Lior Elrom
Lior Elrom

Reputation: 20852

You can always use the short way:

echo (isset($_REQUEST['CustomerName'])) ? "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>" : false;

Logic:

echo (condition) ? TRUE : FALSE;

Upvotes: 0

reikyoushin
reikyoushin

Reputation: 2021

You mean this?

if($_REQUEST['CustomerName']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";
endif;
if($_REQUEST['CustomerEmail']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Email:</span>{$_REQUEST['CustomerEmail']}</div>";
endif;
if($_REQUEST['CustomerPhone']):
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Phone:</span>{$_REQUEST['CustomerPhone']}</div>";
endif;
if($_REQUEST['CustomerAddress']): 
    echo "<div class='reviewItem'><span class='reviewTitle'>Customer Address:</span>{$_REQUEST['CustomerAddress']}</div>";
endif;

Upvotes: 0

Ryan
Ryan

Reputation: 3171

I usually write it like this, which tests for the var being set and not blank:

<?php
if(isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName']!=''){
 echo "<div class='reviewItem'><span class='reviewTitle'>Customer Name:</span>{$_REQUEST['CustomerName']}</div>";
}
?>

Upvotes: 0

Madbreaks
Madbreaks

Reputation: 19539

You've just about got it. Try this:

if(isset($_REQUEST['CustomerName']}))
     echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

PHP's isset checks to see if the referenced variable has a value assigned - if it does, echo it.

Upvotes: 0

peterm
peterm

Reputation: 92785

Try

if (isset($_REQUEST['CustomerName']))
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

or

if (isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName'])
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";

Upvotes: 0

Related Questions