mercredi 19 août 2015

Multiple Values (1 or 0) for checkbox array with same name

Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked. I can't use because of the array. It doubles my array. If I try to set it after using isset it doesn't put it into my array.

I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.

So how can I put a 1 into the array for wherever it isn't checked?

Form:

<?php
require_once ("con.php");


$p=payment();

$result = mysqli_query($mysqli,$p);


while($res=mysqli_fetch_array($result))
{



?>

<tr>

    <td class="paid">

        <input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"

            <?php 
                if($res["paid"]==0)
                {
                echo "checked";
                }               
            ?>>

    </td>
    <td class="active">
        <input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]" 
            <?php 
                if($res["active"]==0)
                {
                    echo "checked";
                }        
            ?> >
    </td>


        <input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">


</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>

</table>
</form>

    </body>
</html>

php:

$paid=$_POST['paid'];
$active=$_POST['active'];


foreach($ID as $key=>$value) 
{
    $ID=$ID[$key];
    $paid=$paid[$key];
    $active=$active[$key];

$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up); 
echo "Information stored successfully";

}

?>

I can see the arrays are coming out fine if everything is checked, but like I said, missing values if not.

Any help would be appreciated!

Thanks!




Aucun commentaire:

Enregistrer un commentaire