PHP表单没有显示正确的错误消息

时间:2013-05-03 11:25:22

标签: php forms

有没有人知道为什么会显示错误消息?我累了空布尔测试。

谢谢!

<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>";
};
?>

3 个答案:

答案 0 :(得分:3)

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

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

翻译为EITHER the TankTopOff is something else than "No" OR TankTopOff is something else than "Yes";这总是如此。

可能您只需要将||替换为&&(或者更好 - “和”,如果some PHP framework developers建议的话)。

答案 1 :(得分: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>";
    };
  ?>

答案 2 :(得分:1)

使用

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

而不是

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

并使用TireReplace

执行相同的操作