jeudi 25 janvier 2018

Get value of multiple checked checkbox in multiple rows php

I have a table where I have multiple rows containing multiple checkbox. enter image description here Each row has unique ID.

Checkbox input codes:

$i=0;
while($row = mysql_fetch_array($query))
{
    echo "
    <tr>
        <td><input type='checkbox' name='checks[0][$i]' value='".$row['ID']."'></td>
        <td><input type='checkbox' name='checks[1][$i]' value='".$row['ID']."'></td>
            <td><input type='checkbox' name='checks[2][$i]' value='".$row['ID']."'></td>
            <td><input type='checkbox' name='checks[3][$i]' value='".$row['ID']."'></td>  
        <td><input type='checkbox' name='checks[4][$i]' value='".$row['ID']."'></td
            <td><input type='checkbox' name='checks[5][$i]' value='".$row['ID']."'></td
            <td><input type='checkbox' name='checks[6][$i]' value='".$row['ID']."'></td>
            <td><input type='checkbox' name='checks[7][$i]' value='".$row['ID']."'></td>
    <td>
        A<input type='checkbox' name='legend[]' value='A'>
            B<input type='checkbox' name='legend[]' value='B'>
        C<input type='checkbox' name='legend[]' value='C'>
        D<input type='checkbox' name='legend[]' value='D'>
        E<input type='checkbox' name='legend[]' value='E'>
        F<input type='checkbox' name='legend[]' value='F'>
        G<input type='checkbox' name='legend[]' value='G'>
        H<input type='checkbox' name='legend[]' value='H'>
        I<input type='checkbox' name='legend[]' value='I'>
        J<input type='checkbox' name='legend[]' value='J'>
        K<input type='checkbox' name='legend[]' value='K'>
        L<input type='checkbox' name='legend[]' value='L'>
        M<input type='checkbox' name='legend[]' value='M'>
    </td>
<tr>";
    $i++;
    }

And here's the code I'm using for getting the checkbox value in each row:

$checks=$_POST['checks'];
$item_count=$_POST['items'];
$legends=$_POST['legend'];  

if(isset($_POST['btnSubmit']))
 {
   for($j=0; $j<$i; $j++)
    {
        if (!empty($checks[0][$j]))
        {
            echo $checks[0][$j];
        }

        if (!empty($checks[1][$j]))
        {
            echo $checks[1][$j] ." ";
        }
        if (!empty($checks[2][$j]))
        {
            echo $checks[2][$j] ." ";
        }
        if (!empty($checks[3][$j]))
        {
            echo $checks[3][$j] ." ";
        }
        if (!empty($checks[4][$j]))
        {
            echo $checks[4][$j] ." ";
        }
        if (!empty($checks[5][$j]))
        {
            echo $checks[5][$j] ." ";
        }
        if (!empty($checks[6][$j]))
        {
            echo $checks[6][$j] ." ";
        }
        if (!empty($checks[7][$j]))
        {
            echo $checks[7][$j] ." ";
        }
        if(!empty($legends))
        {
            $n=count($legends);
            for($cnt=0; $cnt<$n; $cnt++)
            {
                echo $cnt+1;
                echo $legends[$cnt] ." ";   
            }
        }       
    }
}

I can How can I get the value of the checkbox in each row? And how can I shorten my codes?




Aucun commentaire:

Enregistrer un commentaire