2011-08-10 60 views
-3

我希望显示$金额。但这并没有发生。如何使用PHP显示这些值?

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

所以现在在显示的消息中,$ amount没有被显示。为什么?

+1

** **被显示的内容? –

+1

你确定$ _POST ['amount']不是空的吗?试着直接回应,看看你得到了什么。 – Andrew

+0

适用于我:http://codepad.org/HJ9lHTHO。只要'$ _POST ['amount']'不为空,它就可以工作。 –

回答

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;