Retain Form data on reload/refresh (solved)

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Retain Form data on reload/refresh (solved)

Post by Klown »

I am building a trade screen for a game im working on. i need to retain text field data from my POST method form when i reload/refresh the page. This will occur before the submit button is pressed. I have a visual inventory which displays 8 items at a time on the trade screen. then it will display a number of links below the inventory to view other pages of the inventory, such as: 1 2 3 4 5 6 7 i have the links directed at the same page passing pagenum variable in url so it will display whichever page of inventory you wish. the problem is my text fields reset on refresh/reload when this occurs. any ideas would be helpful. I've toyed with the idea of javascript but cant seem to get the text fields to retain data except onfocus. but i wish to have the fields retain the users inputted values without having to focus on them first.

Thanks in advance.

-klown
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Retain Form data on reload/refresh

Post by Jackolantern »

You can steal a page from Microsoft's ASP.NET and basically roll your own "View State". The basics of ASP.NET ViewState is that Javascript on the page adds the values entered into the forms into a hidden form field. What you can do is set an event on the form's submit button. When it is clicked, you first store the values of the form fields into the hidden form field(s). Then you can use Javascript to submit the form, including the hidden field(s) you just added. Then you have the values server-side where they can be issued back to the page and you can set the value attributes (or any other attributes required) with the saved values with PHP when the form refreshes. You would just need to make sure the values are populated with IF statements checking the variables first before trying to assign the variable's values on the page, since they will not exist if it is the user's first time to the form.

Keep in mind that this is a heavy-duty method to retain any and all values and settings you need to "re-hydrate" your forms and data through page refreshes. If all you need are just the same values that were sent to the server through the POST request, you already have the values server-side, and you can repopulate them without any Javascript at all, thus improving the reach of your site.
The indelible lord of tl;dr
Klown
Posts: 90
Joined: Tue Feb 22, 2011 5:59 am

Re: Retain Form data on reload/refresh

Post by Klown »

Thanks for the help.. I actually worked on this for a while and came up with a solution. since it was a trade anyways, i just added a "status" field to the db entry and then turned all the page links under the inventory to submit buttons so it saves to the db until submitted for trade then it updates the status to "send" instead of "save" so now i can repopulate the fields on the form from the db entries.

-klown
if( $myGames != "Crap" ) {
  • please_donate( $money );
} else {
  • just_enjoy();
}
Post Reply

Return to “Beginner Help and Support”