2013-05-03 102 views
-1

有沒有人有任何想法,爲什麼這是顯示錯誤信息?我厭倦了空洞和布爾測試。PHP表單不顯示正確的錯誤信息

謝謝!

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

首先,代之以''errorMessage' $ errorMessage' – 6339 2013-05-03 11:36:22

回答

2

使用

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

代替

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

,並做相同的TireReplace

3

的布爾表達式將永遠不會是假:

(($_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所建議的那樣)。

+0

B他工作但我喜歡空,因爲它不那麼複雜。我不知道爲什麼它第一次不工作...可能太靠近了! – 2013-05-03 14:36:36

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