如何使用PHP显示这些值?

时间:2011-08-10 13:32:01

标签: php

我想要显示$ amount。但那并没有发生。

$amount = $_POST['amount'];
$message = "Thanks. We've got $amount from you.";
echo $message;

所以现在在显示的消息中,$ amount未显示。为什么呢?

1 个答案:

答案 0 :(得分:3)

你的问题可能是没有设置$ _POST ['amount']的值。请记住,$ _POST数组只包含通过页面提交的元素。以这种方式传递的元素:page.php?amount = 2.99将仅存在于$ _GET和$ _REQUEST超全局中。 $ _REQUEST数组是$ _GET和$ _POST数组的合并。我建议添加基本验证或至少是默认值。

$amount = $_POST['amount'] ? $_POST['amount'] : 0; //sets default to 0 if nothing was passed
$message = "Thanks. We've got $amount from you.";
echo $message;

OR:

$message = $_POST['amount'] ? "Thanks. We've got $_POST[amount] from you." : "Sorry, you didn't enter an amount."; //changes the message if no value was passed, otherwise uses your approach but gets the value directly from $_POST
echo $message;