php 计算不正确
php calculation not correct
我已经在别人的帮助下消除了错误,但是由于某种原因计算没有通过,有人有什么想法吗?
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your reservation for <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> room/s</h1>
<h1>And a room type of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>
<?php
if (isset($_POST['pets']))
{
echo 'With Pets';
}
if (isset($_POST['breakfast']))
{
echo ' and Breakfast Buffet';
}
?>
<h1/>
<h1>Your total due will be: <?php echo $_POST['$total']; ?> </h1>
<p> </p>
<h1>
<?php
if ($_POST['numberofrooms'] == "1")
{
$subtotal = ($_POST['quantity'] * 250);
}
elseif ($_POST['numberofrooms'] == "2")
{
$subtotal = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$subtotal = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$subtotal = ($_POST['quantity'] * $breakfast);
}
$smoke = 500;
if ($_POST['smokingpreference'] == 'smoking')
{
$subtotal = $subtotal + $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$total = $subtotal + $pets;
}
}
?>
</h1>
</body>
</html>
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- 0.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- 0.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- 5.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add 0.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add 0.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add .00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
我似乎无法全神贯注地思考应该如何获取输出,因为我的语法很好,但我总是出错。显然,用户需要能够 select 任何组合并获得正确的输出,但我什至找不到错误的原因,因为在语法上它很好。我也会添加 html 部分。
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- 0.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- 0.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- 5.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add 0.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add 0.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add .00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Vacation Rental Response Form</title>
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your Reservation For <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> Room/s</h1>
<h1>With A Room Type Of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
<h1>And <?php echo $_POST['breakfast']; ?> </h1>
<h1>Your Total Due Will Be: <?php echo $_POST['$sub1']; ?> </h1>
<p> </p>
<h1>
<?php
if (isset($_POST['numberofrooms']) == "1")
{
$sub1 = ($_POST['quantity'] * 250);
}
elseif (isset($_POST['numberofrooms']) == "2")
{
$sub2 = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$sub3 = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$sub1 = $sub1 * $breakfast;
$sub2 = $sub2 * $breakfast;
$sub3 = $sub3 * $breakfast;
}
$smoke = 500;
if (isset($_POST['smokingpreference']))
{
$sub1 = $sub1 * $smoke;
$sub2 = $sub2 * $smoke;
$sub3 = $sub3 * $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$sub1 = $sub1 * $pets;
$sub2 = $sub2 * $pets;
$sub3 = $sub3 * $pets;
}
}
?>
</h1>
</body>
</html>
您的问题是您正在尝试访问 $_POST
数组中不存在的索引:
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
pets
的元素是一个复选框,如果不勾选,您将不会在$_POST
中获得宠物元素。所以像这样会更好:
<h1>With/Without <?php echo isset($_POST['pets']) ? 'With' : 'Without'; ?> </h1>
我已经在别人的帮助下消除了错误,但是由于某种原因计算没有通过,有人有什么想法吗?
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your reservation for <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> room/s</h1>
<h1>And a room type of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>
<?php
if (isset($_POST['pets']))
{
echo 'With Pets';
}
if (isset($_POST['breakfast']))
{
echo ' and Breakfast Buffet';
}
?>
<h1/>
<h1>Your total due will be: <?php echo $_POST['$total']; ?> </h1>
<p> </p>
<h1>
<?php
if ($_POST['numberofrooms'] == "1")
{
$subtotal = ($_POST['quantity'] * 250);
}
elseif ($_POST['numberofrooms'] == "2")
{
$subtotal = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$subtotal = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$subtotal = ($_POST['quantity'] * $breakfast);
}
$smoke = 500;
if ($_POST['smokingpreference'] == 'smoking')
{
$subtotal = $subtotal + $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$total = $subtotal + $pets;
}
}
?>
</h1>
</body>
</html>
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- 0.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- 0.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- 5.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add 0.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add 0.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add .00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
我似乎无法全神贯注地思考应该如何获取输出,因为我的语法很好,但我总是出错。显然,用户需要能够 select 任何组合并获得正确的输出,但我什至找不到错误的原因,因为在语法上它很好。我也会添加 html 部分。
</head>
<body>
<form id="form1" name="form1" method="post" action="hw3.php">
<h1> Vacation Rental
</h1>
<p>Last Name:
<label for="lastname"></label>
<input name="lastname" type="text" id="lastname" size="18" maxlength="18" />
</p>
<p>Number of Nights Staying:
<label for="quantity"></label>
<input name="quantity" type="text" id="quantity" size="3" maxlength="3" />
</p>
<p>Number Of Rooms:
<label>
<br />
<input type="radio" name="numberofrooms" value="1" id="NumberofRooms_0" />
1</label>
- 0.00 Per Night<br />
<label>
<input type="radio" name="numberofrooms" value="2" id="NumberofRooms_1" />
2</label>
- 0.00 Per Night <br />
<label>
<input type="radio" name="numberofrooms" value="3" id="NumberofRooms_2" />
3</label>
- 5.00 Per Night</p>
<p>Smoking or Non-Smoking Room:
<label>
<br />
<input type="radio" name="smokingpreference" value="smoking" id="SmokingPreference_0" />
Smoking</label>
- Add 0.00
<br />
<label>
<input type="radio" name="smokingpreference" value="nonsmoking" id="SmokingPreference_1" />
Non-Smoking</label>
</p>
<p>
<input type="checkbox" name="pets" id="pets" />
<label for="pets">Pets</label>
- Add 0.00</p>
<p>
<input type="checkbox" name="breakfast" id="breakfast" />
<label for="breakfast">Breakfast Buffet</label>
- Add .00 Per Room</p>
<p>
<label>
<input type="reset" name="reset" id="reset" value="Reset" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Vacation Rental Response Form</title>
</head>
<body>
<?php
if (($_POST['lastname'] == NULL) || ($_POST['quantity'] == NULL) ||
!isset($_POST['numberofrooms']) || !isset($_POST['smokingpreference']))
{
echo "<h1>Please return to the form and fill out completely</h1>";
}
else
{
?>
<h1>Thanks for the order, <?php echo $_POST['lastname']; ?> Family.</h1>
<h1>Your Reservation For <?php echo $_POST['quantity']; ?> Night/s </h1>
<h1>With <?php echo $_POST['numberofrooms']; ?> Room/s</h1>
<h1>With A Room Type Of <?php echo $_POST['smokingpreference']; ?> </h1>
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
<h1>And <?php echo $_POST['breakfast']; ?> </h1>
<h1>Your Total Due Will Be: <?php echo $_POST['$sub1']; ?> </h1>
<p> </p>
<h1>
<?php
if (isset($_POST['numberofrooms']) == "1")
{
$sub1 = ($_POST['quantity'] * 250);
}
elseif (isset($_POST['numberofrooms']) == "2")
{
$sub2 = ($_POST['quantity'] * 350);
}
else //isset($_POST['numberofrooms']) == "3")
{
$sub3 = ($_POST['quantity'] * 425);
}
$breakfast = 25;
if (isset($_POST['breakfast']))
{
$sub1 = $sub1 * $breakfast;
$sub2 = $sub2 * $breakfast;
$sub3 = $sub3 * $breakfast;
}
$smoke = 500;
if (isset($_POST['smokingpreference']))
{
$sub1 = $sub1 * $smoke;
$sub2 = $sub2 * $smoke;
$sub3 = $sub3 * $smoke;
}
$pets = 200;
if (isset($_POST['pets']))
{
$sub1 = $sub1 * $pets;
$sub2 = $sub2 * $pets;
$sub3 = $sub3 * $pets;
}
}
?>
</h1>
</body>
</html>
您的问题是您正在尝试访问 $_POST
数组中不存在的索引:
<h1>With/Without <?php echo $_POST['pets']; ?> </h1>
pets
的元素是一个复选框,如果不勾选,您将不会在$_POST
中获得宠物元素。所以像这样会更好:
<h1>With/Without <?php echo isset($_POST['pets']) ? 'With' : 'Without'; ?> </h1>