Question

I am trying to make this form with checkboxes get values 1 or 0, sort of a boolean, and UPDATE it on mysql, but I havent found a way yet. Maybe you can help. I'll post the form and the code that receives it. I am sure there might be a conflict of loops, but I tryed everything and couldnt get it working. Tks in advance.

THE FORM

<form action="esconde_cat.php" method="post">
    <table width="300px" align="center" width="140" border="1">
        <tr>
            <td></td>
            <td width="200">Categoria</td>
            <td width="100">Mostrar</td>
        </tr>
        <?php
            include "conecta.php";
            include "verifica.php";

            $sql = "SELECT * FROM categorias ORDER BY ordem";
            $res = mysql_query($sql) or die (mysql_error());
            while ($linha=mysql_fetch_array($res)) {
        ?>
        <tr>
            <td><input name="user[]" type="checkbox" value="true"/></td>
            <td><?=$linha['1']; ?></td>
            <td><?php if ($linha['3'] == TRUE){ echo 'SIM'; } else {echo 'NÃO'; } ?></td>
            </td>
        </tr> 

        <?php 
            }
        ?>
    </table>
    <br/>
    <table align="center">
        <tr>
            <td>
                <input type="submit" name="Delete" type="button" value="Alterar">
            </td>
        </tr>
    </table>
</form>

THE CODE THAT RECEIVES THE FORM AND UPDATE MYSQL

<?php

include "conecta.php";
include "verifica.php";

\\THIS PART WORKS, GETS IF THE CHECKBOX IS CHECKED AND RECORD IT ON DB

if(isset($_POST['user'])) {
    foreach ($_POST['user'] as $key => $read){
        $read =(@$_POST['user']=='0')? $_POST['user']:'1';
        echo $read;
        $sql = "UPDATE categorias SET mostrar='$read' WHERE ordem='$key'";
        $res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
    }
}

\\ON THIS PART OF THE CODE THAT I CANT GET THE VARIABLE TO GET THE
\\VALUE FROM CHEBOXES THAT ARE UNCHECKED RECORDED TO DATABASE

if (!isset($_POST['user'])) {
    foreach ($_POST['user'] as $chave => $leia) {
        $leia =(@$_POST['user']=='1')? $_POST['user']:'0';
        echo $leia;
        $sql = "UPDATE categorias SET mostrar='$leia' WHERE ordem='$chave'";
        $res = mysql_query($sql) or die ("Erro ao excluir") . mysql.error();
    }
}

?>
Was it helpful?

Solution

If a checkbox is left unchecked you will get NO key in the $_POST variable. If it's checked the key will be set and the value will be the value of the checkbox.

Assuming a name of "user[]" works, with something like:

<input type="checkbox" name="user[]" value="aa" />
<input type="checkbox" name="user[]" value="bb" checked="checked" />
<input type="checkbox" name="user[]" value="cc" checked="checked" />
<input type="checkbox" name="user[]" value="dd" />
<input type="checkbox" name="user[]" value="ee" checked="checked" />

You would expect $_POST to be:

array(
    "user" => array(
        "1" => "bb"
        "2" => "cc"
        "4" => "ee"
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top