Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

?>
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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"
    )
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.