Retaining checkbox state on submit of page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virk1711
    New Member
    • Sep 2008
    • 3

    Retaining checkbox state on submit of page

    I want to retain the state of checkboxes after the submit button has been clicked.
    What it is currently doing is that it is resetting the checkboxes after the submit button is clicked.
    My code is given below:-[code=php]
    <?php
    session_start() ;
    ?>
    <html>
    <head>
    </head>
    <body>
    <form name="form1" action="chk10.p hp" method="POST">
    <table>
    <?PHP
    for($j=1;$j<=10 ;$j++)
    {
    ?>
    <tr>
    <td>
    <input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
    $a=$_POST["chk"];
    for($i=1;$i<=10 ;$i++)
    {
    $_SESSION['count'][$i]=$a[$i];
    }
    for($i=1;$i<=10 ;$i++)
    {
    if($_SESSION['count'][$i]==$a[$i])
    {
    echo 'checked="check ed"' ;
    }
    }
    > <?PHP echo $j; ?>
    </td>
    </tr>
    <?PHP
    }
    ?>
    <tr>
    <td>
    <input type="submit" name="btn" value="save" />
    </td>
    </tr>

    <tr>
    <td>
    <a href="session1. php">Show session content</a>
    </td>
    </tr>


    </table>
    </form>
    </body>
    </html>[/code]
    Last edited by pbmods; Sep 1 '08, 02:32 PM. Reason: Added CODE tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Keep in mind that a checkbox will not be sent unless it is checked.
    So using the isset function is a good way to see if a box was selected.
    For example:
    [code=php]
    <?php
    // Check if the box was sent.
    $checked = "";
    if(isset($_POST['box1'])) {
    $checked = 'checked="check ed"';
    }

    // Print the form
    echo <<<END
    <form action="?" method="post">
    <input type="checkbox" name="box1" $checked /> Testbox<br />
    <input type="submit" />
    </form>
    END;
    ?>
    [/code]

    Comment

    • virk1711
      New Member
      • Sep 2008
      • 3

      #3
      Hii,
      Thanks for ur quick reply. Now my exact question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.

      Comment

      • virk1711
        New Member
        • Sep 2008
        • 3

        #4
        Saving checkbox items in a session on click of submit button

        My question is that first i need to create the 10 checkboxes which i have already done and then when i set the value of any of the checkboxes as checked, it should be saved in a session after clicking on the submit button.After clicking on the submit button the values of the checkboxes that were checked should remain checked and should not be reset.And then after checking the values of checkboxes time and again on clicking on submit button, the checked values should be saved in session again such that the duplicate values are not allowed in the session. Since i am novice in php i have tried a lot but without success, please help out.
        The code that i have tried is below:-
        [code=php] code goes here...
        <?php
        session_start() ;
        ?>
        <html>
        <head>
        </head>
        <body>
        <form name="form1" action="chk10.p hp" method="POST">
        <table>
        <?PHP
        for($j=1;$j<=10 ;$j++)
        {
        ?>
        <tr>
        <td>
        <input type="checkbox" name="chk[]" value=<?PHP echo $j ;?> <?PHP if(isset($_POST["chk"]){
        $a=$_POST["chk"];
        for($i=1;$i<=10 ;$i++)
        {
        $_SESSION['count'][$i]=$a[$i];
        }
        for($i=1;$i<=10 ;$i++)
        {
        if($_SESSION['count'][$i]==$a[$i])
        {
        echo 'checked="check ed"' ;
        }
        }
        > <?PHP echo $j; ?>
        </td>
        </tr>
        <?PHP
        }
        ?>
        <tr>
        <td>
        <input type="submit" name="btn" value="save" />
        </td>
        </tr>

        <tr>
        <td>
        <a href="session1. php">Show session content</a>
        </td>
        </tr>


        </table>
        </form>
        </body>
        </html>
        [/code]
        Last edited by Atli; Sep 2 '08, 07:58 PM. Reason: Fixed the [code] tags.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          Do not double post your questions. This only serves to cause confusion and extra work for us moderators.

          I have merged your new thread into this one.

          Please read the Posting Guidelines before posting.

          Thank you.
          MODERATOR

          Comment

          Working...