Remembering if checkbox is checked from dynamically created checkbox array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rod McRae
    New Member
    • Nov 2011
    • 1

    Remembering if checkbox is checked from dynamically created checkbox array

    So I have an array of stings in php, I assign that array to an array of checkboxes. I need away to remember which checkbox was checked whenever I click submit(the page returns to itself for validation, outputting errors if there are any)
    here is my code:

    $courseOffering = array(
    'CST8110 Introduction to Computer Programming 5 hrs/w',
    'CST8209 Web Programming I 5 hrs/w',
    'CST8260 Database System and Concepts 4 hrs/w',
    'ENL1813T Communications I 3 hrs/w',
    'MAT8001 Math Fundamentals 3 hrs/w',
    'MGT8100 Career and College Success Skills 3 hrs/w',
    'CST8250 Database Design and Administration 5 hrs/w',
    'CST8253 Web Programming II 4 hrs/w',
    'CST8254 Network Operating Systems 5 hrs/w',
    'CST8255 Web Imaging and Animations 4 hrs/w',
    'CST8256 Web Programming Languages I 5 hrs/w',
    'CST8257 Web Applications Development 5 hrs/w',
    'CST8258 Web Project Management 3 hrs/w',
    'ENL1819T Reporting Technical Information 4 hrs/w',
    'WKT8100 Cooperative Education Work Term Preparation 15 hrs/w',
    'CST8259 Web Programming Languages II 5 hrs/w',
    'CST8265 Web Security Basics 5 hrs/w',
    'CST8267 Ecommerce 4 hrs/w' );
    ?>


    $count = count($courseOf fering);

    for($i=0; $i < $count; $i++){
    echo("<input type=\"checkbox \" id = \"checkBox\" name= \"courseCheckbo x[]\" value = $courseOffering[$i] >$courseOfferin g[$i]</input>");
    echo("</br>");
    }
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #2
    When you submit a form with checkboxes, the POST (or GET) array will contain the names of the ones that are checked only, so in your for loop you could check if the name of the current checkbox being echoed exists in the POST ( / GET) array and add a "SELECTED" attribute to the input tag.

    That is, if I understand your question correctly. If you mean remembering between sessions then you'll need cookies, but it's a similar concept.

    Also, in the future, please be careful to use code tags when posting code so that the formatting is preserved. It makes code a little easier to read.

    Comment

    Working...