Using PHP, how do I echo a different output based on the total number in a form?
I have a form that calculates a total number using Javascript. The code for the Javascript was taken from here.
Once the user submits the form, he is taken to the next page. On the next page, the PHP displays the form total again (this part works), and then, it is supposed to display a different output based on the range of numbers the total is between.
For instance, if the total is between 1 - 250 (say, 100), then X should be displayed. If the total is between 251 - 500 (say, 350), then Y should be displayed. At the moment, it keeps displaying the same output no matter what the total is.
You can see the form in action here.
Here is the PHP code that I'm using (this will be on the page the user is re-directed to:
<?php session_start();
if ($_POST && !empty($_POST['TOTAL'])) {
$_SESSION['TOTAL'] = $_POST['TOTAL'];
} ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<?php
if (isset($_SESSION['TOTAL'])) {
echo 'Your total volume is ' . round($_SESSION['TOTAL']) . ' Cubic Feet. The total price for your shipment is:';
} else {
echo 'You did not fill out any details. Please go back.';
}
if (isset($_SESSION['TOTAL']) > 0 && isset($_SESSION['TOTAL']) <= 250) { echo ' $1,000';}
if (isset($_SESSION['TOTAL']) > 250 && isset($_SESSION['TOTAL']) <= 500) { echo ' $2,000';}
if (isset($_SESSION['TOTAL']) > 501 && isset($_SESSION['TOTAL']) <= 1000) { echo ' $4,000';}
else { echo 'Sorry, we do not have a price for that.';}
?>
</body>
Thank you in advance.
Do not use isset
for your equations (it returns true
if the value is set and true
is always >0
and <250
). So instead of
if (isset($_SESSION['TOTAL']) > 0 && isset($_SESSION['TOTAL']) <= 250) { echo ' $1,000';}
say
if ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 250) { echo ' $1,000';}
Also use elseif
to ensure, only one block is executed:
if ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 250) {
echo ' $1,000';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 500) {
echo ' $2,000';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1000) {
echo ' $4,000';
} else {
echo 'Sorry, we do not have a price for that.';
}
Note:
This was an expansion of DerVO's first revision. It is largely unnecessary now, but I'm leaving it up for posterity.
Here's an expansion of DerVO's answer:
You're testing isset($variable) > 0
and isset($variable) < #
. Since isset()
returns either true
or false
(or null
, though that can sometimes be treated as false
, as in your case), PHP will attempt to make sense of the following comparison (which is what yours boils down to):
true > 0 && true <= 250
In PHP, as a loosely typed language, true
is often interpreted as 1
. So PHP asks the question, is 1 larger than 0 AND smaller than 250?
. It finds that the answer is yes
and prints your first statement.
All of the other statements it finds to be false, so it skips.
Try checking isset()
in an outer block:
if( isset($_SESSION['TOTAL']) )
{
}
And do your output within that block, skipping any of the isset()
parts.
On line 3, I'd suggest you change
$_SESSION['TOTAL'] = $_POST['TOTAL'];
to
$_SESSION['TOTAL'] = intval($_POST['TOTAL']);
This should eliminate any guessing as to whether the script is seeing your input from Javascript as a string. Then I'd remove the issets from below, it's screwing up your comparisons. You are comparing true to an integer
链接地址: http://www.djcxy.com/p/58576.html上一篇: 循环只插入最后一个值