c_pattle Posted October 9, 2010 Share Posted October 9, 2010 I have a form and on each input I set the value as the post variable of it's self. I do this so that if the user submit the form and it has errors they haven't lost the data they have inputted. For example <input name="input1" type="text" value="<?php echo($_POST['input1']); ?>" /> This works fine for text field and textarea's but how do I retain the value of radio boxes and checkboxes? Link to comment https://forums.phpfreaks.com/topic/215516-retaining-checkbox-values/ Share on other sites More sharing options...
BlueSkyIS Posted October 9, 2010 Share Posted October 9, 2010 within each checkbox or radio tag [code=php:0] echo "<input type='checkbox' name='checked_value' value='$somevalue'"; if ($checked_value == $somevalue) { echo " checked='checked'"; } echo " />"; Link to comment https://forums.phpfreaks.com/topic/215516-retaining-checkbox-values/#findComment-1120663 Share on other sites More sharing options...
Pikachu2000 Posted October 9, 2010 Share Posted October 9, 2010 Since checkboxes only show up in the $_POST array if they're actually checked, you can just check with isset. <input type="checkbox" name="checkbox_name" value="true"<?php echo isset($_POST['checkbox_name']) ? ' checked="checked"' : ""; ?>> Link to comment https://forums.phpfreaks.com/topic/215516-retaining-checkbox-values/#findComment-1120664 Share on other sites More sharing options...
Oziam Posted October 9, 2010 Share Posted October 9, 2010 You have to use checked! e.g <input type=radio name=radio1 checked> and <input type=checkbox name=check1 checked> and if you want to show a select option as selected you use "selected" hope this helps! Link to comment https://forums.phpfreaks.com/topic/215516-retaining-checkbox-values/#findComment-1120666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.