• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Repopulating a form from database after submission

Beanjam

Active member
#1
I'm attempting to create a edit form under Your Account > Settings that allows updating of a custom field I've added to xf_user.

I've extended ControllerPublic_Account using actionPersonalDetails() and actionPersonalDetailsSave() for reference.

But I can't figure out how to make my template's form reload the updated data from the database after submission. It's populating fine on initial load.

My database field is a DECIMAL and when I load the form and enter an integer instead, after submitting the form I get the 'Your changes have been saved' message but it does not repopulate the field with the decimal conversion even though I can see it's been updated in the table. A hard refresh will show the new value, but a simple F5 won't do it.

Is this the expected behaviour?
 

Beanjam

Active member
#2
After having a play around with Your Account > Settings > Personal Details it seems this is actually the default behaviour.

For example, if I enter the Custom Title with whitespace around the text and Save Changes, the form field does not automatically update to show the trimmed value stored in the database.

Could someone please explain why this is the preferred behaviour?
 

Beanjam

Active member
#3
It looks like a caching problem with Firefox. I can replicate it with leading spaces in the AIM field on the Contact Details page. After submitting the form, Firefox won't show the new trimmed value but Chrome does.
 
Last edited:

Beanjam

Active member
#4
I think I'm talking to myself here, but after much head scratching it does seem to be a Firefox issue so I'm putting this here for anyone else having the same problem.

Please disregard the specifics of my project as they are unrelated, but basically I am making a form that submits back to itself (such as the built in Personal Details, Contact Details etc pages).

Normally using data-redirect="on" with the AutoValidator class *should* reload your form's page when submitting and redirecting back to itself, which will in turn repopulate it with the new values from the database (provided you have value="{$your_variable}" set for each field).

But Firefox caches the page, so you don't see your new values returned from the database. :mad:

Chrome and Edge play nicely and will properly reload the page thus updating the form, and as a bonus Chrome seems to keep the scroll position too.

Annoyingly this caching gives the impression that data-redirect="on" is not working in Firefox. Also confusing is that you'll see various data-redirect values used in examples and core code, however all of these (case insensitive) are positive: on, yes, true, 1

Any value (or lack of value) other than those will disable data-redirect in all browsers.

The easiest workaround for this is to use a unique query string for the return URL, which tricks the browser into thinking it's a new page so re-fetches it.

PHP:
return $this->responseRedirect(
    XenForo_ControllerResponse_Redirect::SUCCESS,
    XenForo_Link::buildPublicLink('your_form', null, array('nocache' => uniqid()))
);
While this will force Firefox to reload and repopulate the form, the side-effect is that Chrome will now jump back to the top of the page after submission.