Retain form values ​​after submitting PHP

I forgot to say that there are drop-down menus as well, that I would like to save the selected value

I have a form with checkboxes, radio buttons and several text fields. I know how to save the text box values ​​after the form is submitted, but I would like to keep the switch selection and checkboxes after the form is submitted. I am posting to the same page.

+5
source share
5 answers

To preset radio buttons and checkboxes, you need to add an attribute checked="checked"to the HTML that you create for each control that you want to display.

For example, if you 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" '; ?>
/>

: :

<select name="foo">
  <option value="bar">Text</option>
</select>

selected="selected":

<select name="foo">
  <option value="bar" 
    <?php if(isset($_POST['foo']) && $_POST['foo'] == 'bar') 
          echo ' selected="selected"';
    ?>
  >Text</option>
</select>

, "", ( ).

+13

:

<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>
+3
<input type="checkbox" name="foo" value="foo" <?php if(isset($_POST['foo'])){echo 'checked';} ?>"/>
+3

, . HTML .

HTML "CHECKED", .

+1
<input type="text" name="nazev_projektu" id="nazev_projektu" class="inp" value="<?php if(isset($_POST['nazev_projektu'])) echo $_POST['nazev_projektu']; ?>" />

checked = "checked" ..

<input type="checkbox" ... ="<?php if(isset($_POST['ThisRadioIsChecked'])) echo 'checked="checked"'; ?>" ... />
0
source

All Articles