Get checkbox value in php

Posted on 2008-11-06
Last Modified: 2013-12-13
i have a php file that have some ckeckboxes in table, that are 'checked' or 'unchecked' by database value at the time of form load.
now i want 'check' or 'uncheck' the checkboxes and click a save link that update new 'checked' or 'unchecked' values in database.
i have save link for each database record in HTML table.
Question by:sudanshu
    LVL 48

    Accepted Solution


    1. Named your checkboxes with something like: chk[]
    2. on your php scripts:


    $query = "Delete from yourtable Where id = ".$id;
                $result = mysql_query($query); // or die("Query failed : " . mysql_error());
                //re-insert items checked
                foreach($_POST['chk'] as $chk) {
                      $query = "Insert into yourtable (id, chk_id) Values ($id,$chk) ";
                      $result = mysql_query($query) or die("Query failed : " . mysql_error());


    get the idea? try customize accordingly.
    LVL 107

    Assisted Solution

    by:Ray Paseur
    Note that unchecked checkboxes do not come through in your $_POST variable.  At all.

    So if you have a checkbox in the data base that is checked, and you put up a form on the screen that shows it checked, and the client un-checks it, you will not have anything for that checkbox in $_POST.

    Experiment with the code snippet and you'll see the effect.

    HTH, ~Ray
    <?php // RAY_checkboxes.php
    if (!empty($_POST["_submit"])) {
       echo "<pre>"; var_dump($_POST); echo "</pre> \n";
    <form action="<?=$PHP_SELF?>" method="POST">
    Check or Uncheck Some Boxes Here<br/>
    <input name=box1 type="checkbox"><br/>
    <input name=box2 type="checkbox"><br/>
    <input name=box3 type="checkbox"><br/>
    <input name=box4 type="checkbox"><br/>
    <br />
    <input type="submit" name="_submit"  value="go" />

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now