jrpelt Posted April 12, 2013 Share Posted April 12, 2013 <input id="auction" type="checkbox" name="auction" value="Yes" /> When I submit a checkbox or dropdown the selections don't show. Would someone mind helping me, I'm sure it's simple I just don't know how to go about this. Link to comment https://forums.phpfreaks.com/topic/276850-how-to-keep-checkboxes-checked-and-dropdown-selections-on-submit/ Share on other sites More sharing options...
jrpelt Posted April 12, 2013 Author Share Posted April 12, 2013 this works to keep it checked but on first load I get the undefined index error. <?php // If the box was checked, keep it checked if ( isset($_POST['auction']) && $_POST['auction'] == 'Yes' ) { $checked = 'checked="checked"'; } else { $checked = ''; } ?> <input id="auction" type="checkbox" name="auction" value="Yes" <?php echo $checked ?>/> Link to comment https://forums.phpfreaks.com/topic/276850-how-to-keep-checkboxes-checked-and-dropdown-selections-on-submit/#findComment-1424291 Share on other sites More sharing options...
jrpelt Posted April 12, 2013 Author Share Posted April 12, 2013 <?php // If the box was checked, keep it checked if ( isset($_POST['auction']) && $_POST['auction'] == 'Yes' ) { $checked = 'checked="checked"'; } else { $checked = ''; } ?> <input type="hidden" value="No" name="auction" /><!-- Added this to get rid of undefined index problem --> <input id="auction" type="checkbox" name="auction" value="Yes" <?php echo $checked ?>/> Okay so that took care of my undefined index problem and I now have a checkbox that remains checked after submitting the form. Now I want my drop down menus to retain their selected values. Link to comment https://forums.phpfreaks.com/topic/276850-how-to-keep-checkboxes-checked-and-dropdown-selections-on-submit/#findComment-1424298 Share on other sites More sharing options...
subhomoy Posted April 12, 2013 Share Posted April 12, 2013 you can also do it in this way... <input id="auction" type="checkbox" name="auction" value="Yes" checked="checked"/> Link to comment https://forums.phpfreaks.com/topic/276850-how-to-keep-checkboxes-checked-and-dropdown-selections-on-submit/#findComment-1424311 Share on other sites More sharing options...
mac_gyver Posted April 12, 2013 Share Posted April 12, 2013 for drop-down select menus output selected="selected" inside the <option > tag you want to be selected. Link to comment https://forums.phpfreaks.com/topic/276850-how-to-keep-checkboxes-checked-and-dropdown-selections-on-submit/#findComment-1424340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.