Handling a page reload with dynamic elements


I have a lengthy form to record data for multiple tables. It is a public form so in order to prevent spam I wanted to use recaptcha. The only issue with this form is that I need to refresh the page to do server side verifying. Once I reload the page it removes all of the dynamic elements on the page. For example, I have several HAS-MANY relationships that adds multiple items of the same type. It wouldn’t be an issue if the recaptcha validates without an issue, but I am sure some people will have problems always inserting the right data into the captcha so it reloads the page throwing the error.

My question is: Is there a good way to handle this with MULTIPLE models in one form on one page?

I have researched it and I have came up with either storing items into sessions or cookies, but once I do this it doesn’t keep the same layout. I also thought about setpagestate(), but it only handles one model it seems…

Could I possibly store the values in the database temporarily, and if the recaptcha doesn’t validate delete it after an hour?

Does anyone have any thoughts or opinions of handling this?

Thanks for any comments!


Hi Aaron,

I think any method you mentioned may be used, depends on your specific application needs.

I didn’t get, though, what did you exactly mean by “[color=”#1C2837"][size=“2”]but once I do this it doesn’t keep the same layout". [/size][/color]

[size=“2”][color="#1c2837"]If there’s a definite problem with your current implementation, maybe it’s a good idea to share your problematic code to find the issue.[/color][/size]




Since the site will be a low traffic site, I decided to just put everything into the database and redirect them to another page where they have more attempts at figuring out the captcha. This probably isn’t one of the more optimal solutions on solving this issue, but it is a workaround. I will just use a cron job to delete entries from the database once a day if they are recorded as spam.

When I referred to: “but once I do this it doesn’t keep the same layout”. I meant that once I reloaded the page most of the dynamic DOM elements were not recreated because of the page reload.

I may implement a few other anti-spam regulations later down the road, but I think this will work for right now.

Thanks for the response!


Just to ammend this I ended up using stateful forms to handle this. Piece of cake with that! =)