1

我有一个很好的表单审查页面:

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

问题是我有很多字段没有填写。我想在回显之前放一个“if”语句来检查它是否有数据。如果没有,则不会显示任何内容。我对 PHP 很陌生,所以我不确定我是否可以这样称呼:

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

提前感谢您的帮助。干杯。

4

6 回答 6

1
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>" : "";

试试看。

于 2013-05-03T18:31:26.747 回答
0

你是这个意思?

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;
于 2013-05-03T18:28:46.143 回答
0

您始终可以使用简短的方法:

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

逻辑:

echo (condition) ? TRUE : FALSE;
于 2013-05-03T18:29:53.670 回答
0

尝试

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

或者

if (isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName'])
    echo "<div class='reviewItem'><span class='reviewTitle'>Cusomer Name:</span>{$_REQUEST['CustomerName']}</div>";
于 2013-05-03T18:27:04.917 回答
0

你刚刚得到它。尝试这个:

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

PHP 的isset检查被引用的变量是否有赋值——如果有,就回显它。

于 2013-05-03T18:27:19.300 回答
0

我通常这样写,它测试 var 被设置而不是空白:

<?php
if(isset($_REQUEST['CustomerName']) && $_REQUEST['CustomerName']!=''){
 echo "<div class='reviewItem'><span class='reviewTitle'>Customer Name:</span>{$_REQUEST['CustomerName']}</div>";
}
?>
于 2013-05-03T18:28:19.767 回答