Keep form values after submit PHP
I forgot to say there are drop down menus also that I would like to keep the chosen value of
I have a form with checkboxes, a radio buttons, and a few text fields. I know how to keep the text field values afte开发者_如何学Pythonr form submit, but I would like to keep the radio button selection and checkboxes checked after submit. I am posting to the same page.
To have the radio buttons and checkboxes pre-checked, you need to add the checked="checked"
attribute to the HTML you generate for each control you want to display checked.
For example, if you currently have this:
<input type="checkbox" name="foo" value="bar" />
You want to change it to this:
<input type="checkbox" name="foo" value="bar"
<?php echo empty($_POST['foo']) ? '' : ' checked="checked" '; ?>
/>
Update: For drop down menus, you want to change this:
<select name="foo">
<option value="bar">Text</option>
</select>
To this, which uses selected="selected"
:
<select name="foo">
<option value="bar"
<?php if(isset($_POST['foo']) && $_POST['foo'] == 'bar')
echo ' selected="selected"';
?>
>Text</option>
</select>
Be careful to keep the two "bar" values that appear above synchronized (echoing the options in a loop will help to make sure of that).
You could do this:
<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>
<input type="checkbox" name="foo" value="foo" <?php if(isset($_POST['foo'])){echo 'checked';} ?>"/>
Use the same paradigm that you use for text-boxes for other fields. You just need to set a different HTML property instead of passing some text through a variable.
For both radio boxes and checkboxes, set the HTML property "CHECKED" and they will be checked.
<input type="text" name="nazev_projektu" id="nazev_projektu" class="inp" value="<?php if(isset($_POST['nazev_projektu'])) echo $_POST['nazev_projektu']; ?>" />
You can do the same thing with checked="checked" etc.
<input type="checkbox" ... ="<?php if(isset($_POST['ThisRadioIsChecked'])) echo 'checked="checked"'; ?>" ... />
精彩评论