Problems with session variables on forms with multiple checkboxes

Discussion in 'Programming/Scripts' started by saco721, Apr 25, 2016.

  1. saco721

    saco721 Member HowtoForge Supporter

    I am a php newbie and am trying to get a form to work with multiple checkboxes that retain their values after moving away from the page using session variables, here is the code:
    PHP:
    <?php
    session_cache_limiter
    ('private_no_expire');
    session_start();
    $_SESSION['DB'] = (isset($_POST['check_list']) ? $_POST['check_list'] : (isset($_SESSION['DB']) ? $_SESSION['DB'] : ''));

    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <title>PHP: Get Values of Multiple Checked Checkboxes</title>

    </head>
    <body>


    <h2>PHP: Get Values of Multiple Checked Checkboxes</h2>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <?php
    if(isset($_POST['submit'])){
    if(!empty(
    $_POST['check_list'])) {

    $checked_count count($_POST['check_list']);

    echo 
    "You have selected following ".$checked_count." option(s): <br/>";

    foreach(
    $_POST['check_list'] as $selected) {
    echo 
    "<p>".$selected ."</p>";
    }

    }
    else{
       
       
    echo 
    "<b>Please Select Atleast One Option.</b>";
    }
    }
    ?>

    <label class="heading">Select Your Technical Exposure:</label>
    <input type="checkbox" name="check_list[]" value="C/C++" <?php if(isset($_SESSION['DB'])) { foreach($_SESSION['DB'] as $selected) { if($selected == "C/C++") { echo "checked=\"checked\""; break; }}} ?><label>C/C++</label>
    <input type="checkbox" name="check_list[]" value="Java" <?php if(isset($_SESSION['DB'])) { foreach($_SESSION['DB'] as $selected) { if($selected == "Java") { echo "checked=\"checked\""; break; }}} ?><label>Java</label>
    <input type="checkbox" name="check_list[]" value="PHP" <?php if(isset($_SESSION['DB'])) { foreach($_SESSION['DB'] as $selected) { if($selected == "PHP") { echo "checked=\"checked\""; break; }}} ?><label>PHP</label>
    <input type="checkbox" name="check_list[]" value="HTML/CSS" <?php if(isset($_SESSION['DB'])) { foreach($_SESSION['DB'] as $selected) { if($selected == "HTML/CSS") { echo "checked=\"checked\""; break; }}} ?><label>HTML/CSS</label>
    <input type="checkbox" name="check_list[]" value="UNIX/LINUX" <?php if(isset($_SESSION['DB'])) { foreach($_SESSION['DB'] as $selected) { if($selected == "UNIX/LINUX") { echo "checked=\"checked\""; break; }}} ?><label>UNIX/LINUX</label>
    <input type="submit" name="submit" Value="Submit"/>

    </form>

    </body>
    </html>
    The problem is that even if no checkboxes are checked after the form has been submitted, the checkboxes still pick up the last values selected rather than being blank. Thank you for your time!.
     
  2. saco721

    saco721 Member HowtoForge Supporter

     
  3. saco721

    saco721 Member HowtoForge Supporter

    Hi,
    Think I sorted the problem by unsetting the session variable.
     
  4. saco721

    saco721 Member HowtoForge Supporter

    Adding unset session seems to improve the script, but its still not working correctly.
     

Share This Page