如果POST不为空,则显示文本

时间:2015-03-10 12:26:59

标签: javascript php variables post

好的,所以我有这个页面,里面有一些价格和东西。但最后你可以以百分比的形式给予折扣。

然后通过POST将此百分比发送到新页面。在这里,我需要它来显示“你已经给予50%的折扣。

但如果没有给出折扣且POST中的百分比字段为空,则它不能显示文本。

现在我得到了类似的东西

$procent .= $_POST['percent_discount'];



$text .= 'You have recived a discountf of';

$test = $text . $procent;

但无论如何都会显示文字。有关如何使它只显示文本和百分比(如果在POST中发送百分比)的任何想法吗?

5 个答案:

答案 0 :(得分:4)

您可以使用isset()来检查值。像这样:

if(isset($_POST['percent_discount'])){
    // do something if its set here
}else{
    // do something if its not set
}

与其他答案略有不同,如果出现以下情况,您也可以使用简写:

$myString = (isset($_POST['percent_discount']) ? "You received " .$_POST['percent_discount'] . "!" : "We don't like you. No discount for you!");

等...

希望这有帮助!

答案 1 :(得分:1)

您需要使用empty()函数来查看它是否已设置,或者您还可以使用isset()

 if (empty($_POST))
        //do your no post thing
 else
        //do your post thing

 //using isset
 if (isset($_POST['percent_discount'])
        //post is set
 else
        //post is not set

答案 2 :(得分:0)

在将文本添加到输出之前,使用if语句检查帖子字段是否为空。

if( !empty( $_POST['percent_discount'] ) ) {
    $text .= 'You have recived a discountf of' . $procent;
}

如果帖子字段始终存在,如果有时可能根本没有设置字段,则可以使用isset

if( isset( $_POST['percent_discount'] ) && !empty( $_POST['percent_discount'] ) ) {
    $text .= 'You have recived a discountf of' . $procent;
}

希望有所帮助

答案 3 :(得分:0)

$procent = isset($_POST['percent_discount'])? $_POST['percent_discount'] : 0;

答案 4 :(得分:0)

您必须在何时打印文本或何时不

例如

if(isset($_POST['percent_discount']))
{
    echo 'This is text, it\'ll be shown if the discount is given.';
}

通过使用条件,它只会在给出折扣时显示文本。

希望有所帮助:)

相关问题