2012-05-14 43 views
0

我在這種情況下有問題,我搜索了很多,不知道該怎麼辦... 我有4個複選框,每個複選框都有一定的功能正在執行中。我對這個複選框的HTML代碼,在PHP中訪問複選框的值和使用函數

<form action="check.php" method="post"> 
<table border="1" width="200"> 
<tr> 
<td> 
<input type="checkbox" name="first[]" value="option1" /> 
</td> 
<td width="500"> 
<strong>Option 1</strong> 
</td> 
</tr> 
<tr> 
<td> 
<input type="checkbox" name="first[]" value="option2" /> 
</td> 
<td width="500"> 
<strong>Option 2</strong> 
</td> 
</tr> 
<tr> 
<td> 
<input type="checkbox" name="first[]" value="option3" /> 
</td> 
<td width="500"> 
<strong>option 3</strong> 
</td> 
</tr> 
</table> 
<table border="1" width="200"> 
<tr> 
<td> 
<input type="text" name="entered_value"/> 
</td> 
</tr> 
</table> 
<table border="1" width="200"> 
<tr> 
<td> 
<input type="submit" value="Submit"/> 
</td> 
</tr> 
</table> 
</form> 

我使用此代碼訪問這些複選框

<?php 
if (isset($_POST['submit'])) 
    { 
     if(isset($_POST['first'])) 
      { 
       $get_data_checkboxes = $_POST['first']; 
      } 
     else 
      { 
       $get_data_checkboxes = ""; 
      } 
      print_r($get_data_checkboxes); 
      exit(); 
} 
?> 

我所面臨的問題是,正如我需要運行打勾每個複選框的功能,例如如果人蜱選項1的話,我應該能夠運行功能1等等...

任何幫助將不勝感激:)

回答

1

如果您想選擇多個複選框,你可以嘗試這樣的事情:

for($i = 0; $i < count($_POST['first']); $i++) { 

    if($_POST['first'][$i] == 'option1') { 

     echo "function1"; 

    } else if($_POST['first'][$i] == 'option2') { 

     echo "function2"; 

    } else if($_POST['first'][$i] == 'option3') { 

     echo "function3"; 
    } 
} 
+0

非常感謝!它現在工作:) –

0

你不需要行

if (isset($_POST['submit'])) 

沒有你的代碼工作正常,你可以用它來調用您的函數是這樣的:

<?php 
    if(isset($_POST['first'])) 
    { 
     $get_data_checkboxes = $_POST['first']; 
     foreach($get_data_checkboxes as $value){ 
      //call functions named option1(), option2(), etc.. 
      call_user_func($value); 
     } 
    } 
    else{ 
     $get_data_checkboxes = ""; 
    } 

?>