By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Remembering if checkbox is checked from dynamically created checkbox array

P: 1
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($courseOffering);

for($i=0; $i < $count; $i++){
echo("<input type=\"checkbox\" id = \"checkBox\" name= \"courseCheckbox[]\" value = $courseOffering[$i] >$courseOffering[$i]</input>");
echo("</br>");
}
Nov 29 '11 #1
Share this Question
Share on Google+
1 Reply


HaLo2FrEeEk
100+
P: 404
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.
Nov 30 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.