z.abasi z.abasi - 3 months ago 7
SQL Question

How to understand who checkbox is selected?

i want to select a lot row of my database and have checkbox to any of rows that the user can choose any of them.then i should update database according to user choice, but i can't understand who of this checkbox is selected.

thanks a lot...
the semicode is here:

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

}

Answer

Try some thing like this:

Provide a class to all checkboxes:

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

In 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
    }
});