I am trying to find what the easiest way to keep form values after post. I am really trying to have to keep from learning ajax right this second for the one form, Plus I am not sure how hard it would be to implement ajax into my already existing google map page. So I am trying to find a way to retain the values of two date fields after submit has been pushed
Asked
Active
Viewed 1.3e+01k times
32
-
4See: http://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission – Kevin Peno Mar 04 '11 at 19:26
-
wait are you trying to re-populate the form after a post or are you trying to reuse the values in the form after submit? like on different pages? – KJYe.Name Mar 04 '11 at 19:27
-
Do you want to just maintain access to the values? Or do you want the page to persist the values (assuming you are posting to the same page)? – Babak Naffas Mar 04 '11 at 19:28
-
I would like to either re-populate or keep the values there. I assume that to keep the values I have to use Ajax, so I would like to repopulate so the user doesnt have to remember what their selection was. – shinjuo Mar 04 '11 at 19:33
-
1Shoudn't be marked as duplicate with the other question which belongs to wordpress. – Karl Adler Sep 09 '15 at 07:56
2 Answers
72
If you are looking to just repopulate the fields with the values that were posted in them, then just echo the post value back into the field, like so:
<input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" />

Infotekka
- 10,307
- 2
- 20
- 17
-
-
8It would be helpful if you would say what to do, not just "NO. Don't do that." Use htmlspecialchars($_POST[ .. – dcromley Sep 07 '15 at 02:46
-
That's the basic idea. In addition you should clean the submitted data to get rid of coding characters. – Button 108 Oct 23 '15 at 16:06
-
-
It works in the `select` tag. add a second ` ` where the select - option text would go. – grantiago Nov 16 '22 at 16:17
7
you can save them into a $_SESSION variable and then when the user calls that page again populate all the inputs with their respective session variables.

Naftali
- 144,921
- 39
- 244
- 303
-
5storing them into a $_SESSION would kill the server. cookies or the chosen answer are the best ways. – zeeks Sep 03 '14 at 16:30
-
3@zeeks I'm a beginner in php. Why will it kill the server? Could you please clarify? – Ilya Kushlianski Dec 17 '17 at 20:09