Keep form values after submit PHP
Asked Answered
L

5

8

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 after form submit, but I would like to keep the radio button selection and checkboxes checked after submit. I am posting to the same page.

Linville answered 1/4, 2011 at 14:41 Comment(0)
A
15

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).

Alkanet answered 1/4, 2011 at 14:44 Comment(6)
You actually don't need to set a value to "checked"; just set the property.Sanitize
@ashes999: Just checked is not valid markup if you are using an XHTML doctype, while checked="checked" is valid everywhere.Alkanet
You always need to set the value. It is the attribute name, the = and the quotes that are optional in HTML 5 and recommended against in HTML 4. In XHTML they are required though.Collar
@Jon: I forgot to say there are drop down menus also that I would like to keep the chosen value of. DO you know how you would do thatLinville
No one should be using XHTML anymore. Just throw <!DOCTYPE html> in and you're done. I don't know of a single browser that does anything more than go to standards mode if it sees a doctype (ANY doctype) and quirks mode if it doesn't. Even IE6 recognizes the html5 doctype.Curt
@ChrisSobolewski: It's irrelevant if anyone uses it. The point is that this is a fire-and-forget way of covering all bases, so why consider any other approach?Alkanet
M
3

You could do this:

<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>
Metallist answered 1/4, 2011 at 14:45 Comment(0)
C
3
<input type="checkbox" name="foo" value="foo" <?php if(isset($_POST['foo'])){echo 'checked';} ?>"/>
Curt answered 1/4, 2011 at 14:45 Comment(1)
If there is a checked attribute, then it must have the value checked. If you set it to true or false then it is invalid (and you won't get the desired behaviour by setting it to false).Collar
S
1

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.

Sanitize answered 1/4, 2011 at 14:45 Comment(0)
V
0
<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"'; ?>" ... />
Viewer answered 1/4, 2011 at 14:43 Comment(9)
Don't spit unsanitized data into the page. XSS is not pretty. This doesn't address the question (which is about radio buttons and checkboxes) either.Collar
I was actually answering ONLY that elements checked part. Security is another concern.Viewer
Security is an essential part of the solution, it isn't an optional extra.Collar
Yes I understand what are you talking about but you basically say, that every answer should be an complex solution => solving everything. I don't think, that this is always true (At least I haven't got answers which would guide me through complete process while telling me what to write line after line). Yes, the security here is important. But I answered only the part he was asking. You could have give him much more complex answer (with security and all other factors), instead of commenting every person that is trying to move him forward in dealing with the problem.Viewer
Or at least first give him better solution, then start being a critic. (I just have to note something here to make things clear: I accept your comment and agree that security here is missing and you are making right point. I just disagree with your way of reacting.)Viewer
I'll settle for upvoting the better solution.Collar
Interesting that at the moment, all solutions here are same. Nevermind, you obviously got some problem with me :)Viewer
No, not all the solutions are the same. Yours is the only one that spits data directly into the document from $_POST.Collar
Yes, the initial example does this. Ok then.Viewer

© 2022 - 2024 — McMap. All rights reserved.