2016-08-16 63 views
0

我想選擇我的數據庫的很多行,並有用戶可以選擇任何行的任何行的複選框。然後我應該更新數據庫根據用戶的選擇,但我不明白這個複選框是誰選擇。如何理解選中的複選框?

非常感謝...... 的semicode是在這裏:

$query = "Select * mytable limit 30;";   
$res = $mysql->ExecuteStatement (array()); 
echo '<form method="post" action=""> '; 
$b=0; 
while ($rec=$res -> fetch()) 
{ 
    echo '<input type="checkbox" id="$b" name="checkboxName[]" value="yes"/>'; 
    $checkboxID = "number_".$rec["ID"].":"; 
    echo $checkboxID; 
    echo $rec["column1"]."</br>"; 
    $b++; 
} 

if(isset($_POST['formSubmit'])) //this code just get us how many of checkbox are chosen not who are chosen 
{ 
    foreach($_POST['checkboxName'] as $selected) 
    { 
     echo $selected."</br>"; 
    } 

} 

回答

1

嘗試一些事情是這樣的:

提供一個類的所有複選框:

<input type="checkbox" class="MyChkbox" id="$b" name="checkboxName[]" value="yes"/>'; 

在JS:

$('.MyChkbox').click(function(){ 
    if($(this).is(':checked')) 
    { 
     var checkedId = $(this).attr('id'); 
     // checkedId will contain the checked checkbox id in it. Use it as per your need 
    } 
}); 
0

喜在這些情況下,你應該通過行的IdCheckbox值,然後讓他們當formposted,所以你可以試試這個

echo '<td><input type="checkbox" class="MyChkbox" id="$b" name="checkboxName[]" value="' . $rec["ID"] . '"></td>'; 

這些都會valuerowID