SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    none
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Store checkbox values in session display checked checkboxes

    Hello,

    I am trying to store values of checkboxes in SESSION.

    I have a list of options which is generated from the database values:

    PHP Code:
    PHP Code:
    <?php
    $result
    =mysql_query("SELECT * FROM table");
    while (
    $row=mysql_fetch_assoc($result)) {
        
    $d_id=$row['id'];
        
    $d_name=$row['name'];
    <
    input type="checkbox" name="checkbox[]" value="<?=$d_id?>><?=$d_name?></option>
    <?php
    }
    ?>

    when I submit the form (which includes this option list) If there was a mistake I want to return checked checkboxes (the ones that were checked before form submission).
    If everything goes well then I want to put checkbox values into the database.. I guess using implode to comma separate them, but this is another story. I guess If I'll know how to do this with the Session values I'll be then able to get the values from database and pre check the checkboxes.


    so can anyone help with the session thing? I need help with php code :\ i am a newbie...

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the form submit you will have access to the POST variable: checkbox. checkbox will be an array so you can do something like this:

    PHP Code:
    if (isset($_POST['checkbox']) && is_array($_POST['checkbox'])) {
      foreach(
    $_POST['checkbox'] as $value) {
        
    //echo $value -- will output the value from the input="checkbox"
      
    }

    Follow me @YaderHernandez


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •