On a text input, this is how yo开发者_Go百科u would "remember" the value the user entered if the form gets submitted to itself, for say a picture upload tool which requires that, so the user wont have to type everything into the form again after uploading a picture.
<input type="text" id="text" name="text" value="<?php echo @$_POST['text'];?>">
but how is this done when it comes to radios?
I would prefer not to create the actual radio with php, I would prefer another solution. But in the end I would go with the easiest! Javascript is also okay to use here!
Thanks
<input type="radio" id="radio_button_1" name="radio_button" value="1"<?php if($_POST['radio_button'] == 1) { print ' checked="checked"'; } ?> />
<input type="radio" id="radio_button_2" name="radio_button" value="2"<?php if($_POST['radio_button'] == 2) { print ' checked="checked"'; } ?> />
To short further, you can write this statement as:
<input type="radio" id="radio_button_2" name="radio_button" value="2" <?=isset($_POST['radio_button']) ? "checked":"" ?> />
I arrived here via Google and the acccepted answer is missing the if(isset($_POST['radio_button']))
check first, which could be handy in some cases.
<input type="radio" id="radio_button_1" name="radio_button" value="1"<?php if(isset($_POST['radio_button']) && $_POST['radio_button'] == 1) { echo ' checked="checked"'; } ?> />
<input type="radio" id="radio_button_2" name="radio_button" value="2"<?php if(isset($_POST['radio_button']) && $_POST['radio_button'] == 2) { echo ' checked="checked"'; } ?> />
精彩评论