2013-10-05 23 views
0

我在試圖弄清楚如何獲取用於switch語句的單選按鈕的值時遇到了困難。基本上,當用戶選擇其中一個單選按鈕時,我希望執行該單選按鈕的操作。不知道我是否正確設置了這個。我自己學習PHP,不知道這是否是正確的方法。以下是HTML和PHP。獲取用於在PHP中的switch語句中使用的單選按鈕值

<input class="radio" type="radio" name="calculate" value="average" checked="checked">Average<br /> 
<input class="radio" type="radio" name="calculate" value="total">Total<br /> 
<input class="radio" type="radio" name="calculate" value="both">Both<br /> 

這裏是PHP

$calculate_type = $_POST['calculate']; 
    switch ($calculate_type) { 
     case '$calculate_type == "average"': 
      $score_average = $score_total/count($scores); 
      break; 
     case '$calculate_type == "total"': 
      $score_total = $scores[0] + $scores[1] + $scores[2]; 
      break; 
     case '$calculate_type == "both"': 
      $score_average = $score_total/count($scores); 
      $score_total = $scores[0] + $scores[1] + $scores[2]; 
      break; 
    } 
+0

嘗試:'$的情況下= calculate_type =「平均」:'刪除引號 –

回答

2

到底從哪裏,你要學會寫switch statment這樣???再次

switch($calculate_type) { 
    case "average": 
    // do something 
    break; 
    case "total": 
    // do something else 
    break; 
    case "both": 
    // do something completely different 
    break; 
    default: die("Invalid type"); 
} 

那麼,在這種情況下,它會是更好的以下內容:

HTML:

<input class="radio" type="radio" name="calculate" value="1" checked="checked">Average<br /> 
<input class="radio" type="radio" name="calculate" value="2">Total<br /> 
<input class="radio" type="radio" name="calculate" value="3">Both<br /> 

PHP:

if($_POST['calculate'] & 1) $score_average = $score_total/count($scores); 
if($_POST['calculate'] & 2) $score_total = $scores[0]+$scores[1]+$scores[2]; 
相關問題