lantern has asked for the wisdom of the Perl Monks concerning the following question:

Hey hey,

So here's the thing: I'm developing a cgi application for my pco group at work, it's an online location board. The main page maintains a list of users and their locations etc, and its updated by a form on the same page using post method.

I want users to be able to hit reload (to update the displayed data) without resending the information that was last sent by the browser. There must be a way but I haven't figured out how yet. Is this a Perl or HTML issue (or both)?

Thanks in advance,
lantern

Replies are listed 'Best First'.
Re: CGI Reload Forms
by bear0053 (Hermit) on Feb 19, 2004 at 18:33 UTC
    im not sure of anyway to prevent the information from being resent but here are some suggestions:

    1. drop a cookie for each visitor and then if they submit information signify that in the cookie value somehow. this way when the page is loaded it will check the cookie to see if the info that is being sent was already submitted. if it was then it ignores it and simply displays the messages otherwise it will insert the submitted information bc it wasn't submitted before then update the visitor's cookie to show that they already submitted the information

    2. place a link on the page that says refresh posts (or something) this link will invoke the script to just display the posts without submitting anything

    hope this helps
Re: CGI Reload Forms
by cees (Curate) on Feb 19, 2004 at 19:44 UTC

    A simple way of handling this is to issue an external redirect once the new data has been successfully saved on POST. This will force the browser to GET the page again, and it will be safe to use the 'Reload' button.

    In other words, the user POSTs some new data, you save the data, then send a 'Location: ' header that GETs the same page again instead of redisplaying the page as part of the POST request. This way the browser will always display the page with a GET request.

    It means an extra trip back to the browser, but it will solve your problem. The user may have some dificulty if they start playing with the Back button though...

    - Cees

Re: CGI Reload Forms
by injunjoel (Priest) on Feb 19, 2004 at 18:39 UTC
    Greetings all,
    Just to make sure Im clear on what is happening: You have a page with a list of locations and a form to update it (if needed) and currently when you hit the refresh button it asks you if you would like to repost the data that was just sent... sound about right?
    If so this is most definitely a HTML issue. I would suggest having another button on the page that when hit will simply hit the current URL, this assumes that you have no configuration params in the URL or that you can easily recreate them (you mentioned the form has a post method), my suggestion is to use JavaScript for this. That way you are not triggering the reposting of your form by hitting the browser refresh button.
    Just a thought, Hope that helps.
Re: CGI Reload Forms
by jdtoronto (Prior) on Feb 19, 2004 at 18:52 UTC
    Put a second form on the page with a 'reload' button, and nothing else. No other way around it really.

    jdtoronto

Re: CGI Reload Forms
by bean (Monk) on Feb 19, 2004 at 20:27 UTC
    This is an html issue. Unfortunately, what you want to do is contrary to standard browser/form/html behavior, so any solution will be some sort of hack. A reload button or link is the cleanest, easiest solution, but doesn't allow the user to use the reload button on his/her browser. Allowing the form to be resubmitted (and ignoring it) is a bad idea - it will be confusing to your users, whose browsers will ask (every time) if they really want to resubmit the information (plus, it's a lot of work). Redirecting after the form submission is also bad because it hoses the back button (although you might be able to change the behavior of the back button with javascript - you could probably get some inspiration from porn popups on how to do that). Plently of websites hose the back button in this way, but that doesn't make it less of a sin.

    There is a horrible hack to do exactly what you want, but it involves frames (iframes might work) and javascript. Basically, you have to subvert the standard form submission process - instead of having your form's submit button actually submit the form, have it set off some javascript (using onClick) in another frame (iframe?) - we'll call that frameB - that submits a form (from frameB) with the information from the original frame (frameA - the one with the form the user filled out). When frameB comes back from the server, it reloads frameA using javascript (you may have to shift focus back to the original window, again with javascript). The back button works - the reload button works - and there is no annoying "are you sure you want to resubmit the form" warning message. Since you are coding an application, frames are not a Bad Thing.

    If you don't have frames in the application already, I'd just do the reload button/link (although you could see if the javascript/frames thing works with iframes - I can't see why it wouldn't)...
      redirecting only breaks the browsers back button when you use a meta or javascript redirect on the html page. when using a http-header redirect there will be only one page in the browsers history.