How to prevent form fields from repopulating after clicking the back button?

后端 未结 3 1233
一整个雨季
一整个雨季 2020-12-18 11:00

I have a simple form which has four fields named firstName, lastName, address and phone number.

After a user fills this form and clicks the submit button, if everyt

相关标签:
3条回答
  • 2020-12-18 11:27

    Repopulating form fields is a good thing, stop trying to break it.

    If what you actually want is to prevent duplicate submissions, send a unique id (e.g. UUID) along with the form and keep track of the ones you've received recently (how many to keep track of depends on your application).

    If you receive a duplicate you can either ignore it (and display appropriate message), or go a step further: check whether the received data has already been submitted or whether it's an attempt to change the previous submission (i.e. fixing a typo), or to create a new record (maybe firstname and phone were changed), or prompt the user to choose, or whatever.

    0 讨论(0)
  • 2020-12-18 11:41

    Repopulating the FORM fields is a good think i know but we can disable it by using autocomplete="off"

    0 讨论(0)
  • 2020-12-18 11:49

    This works when I run it. First, file testform.cfm

    <cfsetting showdebugoutput="no">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0    
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <CFHEADER NAME="Cache-Control" VALUE="no-cache, no-store, must-revalidate">
    <!--- this is meant for legacy HTTP 1.0 servers 
    - only prevents caching when used with secure communications (https://)  --->
    <CFHEADER NAME="Pragma" VALUE="no-cache">
    <!--- this doesn't prevent caching, 
    just means for future requests that browser must contact server for fresh copy. 
    cached copy used for BACK and FORWARD buttons--->
    <CFHEADER NAME="Expires" VALUE="-1">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form action="formtarget.cfm" method="post">
    <input type="text" name="x" value="" />
    <input name="submitbutton" type="submit" />
    </form>
    </body>
    </html>
    

    This is formtarget.cfm

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <cfdump var="#form#">
    </body>
    </html>
    

    We have those three cfheader tags in a custom tag.

    0 讨论(0)
提交回复
热议问题