Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:29:09+00:00 2026-05-28T18:29:09+00:00

I am trying to make this form with checkboxes get values 1 or 0,

  • 0

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

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:29:11+00:00Added an answer on May 28, 2026 at 6:29 pm

    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"
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make this form: And this is what I get: (
i'm trying to make this form sticky but it's not doing. What i'm trying
im trying to make Categories and Subcategories, im checking this models but i have
I'm trying to make some ajax-functionality in my web application, but I cannot get
I am trying to make a small pizza order form, but I have a
I am trying to make some ajax rendering when the form is submitted, but
I'm trying to make seo to search queries. i've got a form like this:
I am trying to make this landscape only iphone app. I only use this
I'm trying to make this fragment work: Version History --------------- These are the versions
I have been trying to make this to be a little jQuery plugin that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.