0

Below is the code for the checkbox

foreach($fruits as $key=>$values) {
 echo "<td bgcolor=\"#EAEAEA\" style=\"color:#003399\"><input type=\"checkbox\" name=\"fruits[]\" value='$fruits[$key]'>$fruits[$key]</td>";

How can i retain the values selected once i click submit?

Maddy
  • 53
  • 8

1 Answers1

0

Checkboxes don't use value attributes. You have the attribute checked='checked' or simply checked which means it's selected. You then get a $_POST array where if you have the checkboxes checked, they will show up with the names in the keys, but if not checked, they will not be submitted at all. So you would need to make hidden fields if you really want to pass these unchecked checkboxes as well. See this post on the details of how to accomplish it: Post the checkboxes that are unchecked

Community
  • 1
  • 1
TurtleTread
  • 1,297
  • 2
  • 12
  • 23
  • Making a correction I think someone mentioned here before checkboxes actually have value attributes which will be passed as the value associated with the key just like text input boxes. Though only checked ones will be submitted. – TurtleTread Jun 02 '17 at 19:08