2013-08-31 107 views
0

我想擺脫索引undefined。每次點擊提交而未勾選框,我都會收到未定義的索引錯誤。

<html> 
    <head> 
     <title>Order</Title> 
      <style> 
      </style> 
     <body> 
      <form action = "order.php" method = "post"> 
       Coffee:<p> 
       <input type = "checkbox" value = "coffee" name = "cappuccino"/>Capuccino<br> 
</form> 
     </body> 
    </head> 
</Html> 

<?php 
    $capuccino = 3.75; 
    if(isset($_POST["submit"])) 
    { 
     if($_POST['cappuccino'] <> 'coffee') 
     { 
      $capuccino = 0; 
     } 
    } 
?> 
+1

使用'isset'? –

回答

1

嘗試用isset

<?php 
    if(isset($_POST["submit"])) 
    { 
     if(isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee') 
     { 
      $capuccino = 0; 
     } 
    } 
?> 

您還可以使用!=代替<>

$_POST['cappuccino'] != 'coffee' 
+0

+1 for!=,老實說我甚至都不知道<>可以使用;-) – hynner

+0

並告訴我誰降職的原因 – Gautam3164

1

在HTML表單,一個價值沒有得到公佈,如果你不檢查。你應該測試是否是第一次發佈,所以你的PHP代碼將是這樣的:

<?php 
if(isset($_POST["submit"])) 
{ 
    if(isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee') 
    { 
     $capuccino = 0; 
    } 
} 
?> 
0

你的條件應該是:所有輸入驗證也許

if(array_key_exists('cappuccino', $_POST) && isset($_POST['cappuccino']) && $_POST['cappuccino'] <> 'coffee') 
0
<html> 
    <head> 
     <title>Order</Title> 
      <style> 
      </style> 
     <body> 
      <form action = "order.php" method = "post"> 
       Coffee:<p> 
       <input type = "checkbox" value = "coffee" name = "cappuccino"/>Capuccino<br> 
<input type="submit" name="submit" value="Submit" /> 
</form> 
     </body> 
    </head> 
</Html> 

<?php 
    $capuccino = 3.75; 
    if(isset($_POST["submit"])) 
    { 
     if(isset($_POST['cappuccino']) && !empty($_POST['cappuccino']) <> 'coffee') 
     { 
      $capuccino = 0; 
     } 
    } 
?>