0

有谁知道为什么这会显示错误消息?我已经厌倦了空和布尔测试。

谢谢!

<p>You have nearly a full tank of fuel at <?php echo $intFuelGallons ?> gallons.</p>
    <p> Do you want to top off your tank?
      <label>
        <input type="radio" name="TankTopOff" value="Yes" id="TankTopOff">
        Yes</label>
      <label>
        <input type="radio" name="TankTopOff" value="No" id="TankTopOff">
        No</label>
    </p>
    <p><?php echo $varWornTires ?> of your tires are worn. Do you want to replace any of them?</p>
    <label>
      <input type="radio" name="TireReplace" value="Yes" id="TireReplace">
      Yes</label>
    <label>
    <label>
      <input type="radio" name="TireReplace" value="No" id="TireReplace">
      No</label>
    <label>
 <?php
 if ((($_POST['TankTopOff'])!=="No") || (($_POST['TankTopOff'])!=="Yes")) {
 $errorMessage .= "<li><h5>You forgot to choose if you want to top off your tank! " . ($_POST['TankTopOff']) . "</h5></li>";
};
 if ((($_POST['TireReplace'])!=="No") || (($_POST['TireReplace'])!=="Yes")) {
 $errorMessage .= "<li><h5>You forgot to choose if you want to replace any tires! ". ($_POST['TireReplace']) . "</h5></li>";
};
?>
4

3 回答 3

3

布尔表达式永远不会为假:

(($_POST['TankTopOff'])!=="No") || (($_POST['TankTopOff'])!=="Yes")

翻译为EITHER the TankTopOff is something else than "No" OR TankTopOff is something else than "Yes";这总是正确的。

可能您只需要替换||&&(或者甚至更好 - “和”,如果按照某些 PHP 框架开发人员的建议)。

于 2013-05-03T11:32:53.917 回答
1

你应该做

 <?php
   if ($_POST['TankTopOff']=='') {
        $errorMessage .= "<li><h5>You forgot to choose if you want to top off your tank! </h5></li>";
    };
   if ($_POST['TireReplace'])=='') {
        $errorMessage .= "<li><h5>You forgot to choose if you want to replace any tires!</h5></li>";
    };
  ?>
于 2013-05-03T11:36:36.463 回答
1

利用

if(empty($_POST['TankTopOff']))

代替

if ((($_POST['TankTopOff'])!=="No") || (($_POST['TankTopOff'])!=="Yes"))

对 TireReplace 做同样的事情

于 2013-05-03T11:48:19.020 回答