如何阻止表单重新提交

时间:2014-12-09 07:28:02

标签: coldfusion

我想在网站上添加其他功能,包括只能提交一次表单的功能,这样当用户刷新时,所有内容都不会再次处理等。

我已经使用Jquery和javascript查看了几段代码但是想知道在Coldfusion中是否有任何方法可以做到这一点,以及其他使用Coldfusion的人如何做到这一点?如果可能的话,我正在寻找最简单而有效的解决方案。

以下是我要禁用的表单示例"重新提交" funtionality:

<cfinclude template="header.cfm">
<cfparam name="form.step" default="1">
<cfparam name="form.submit" default="">
<cfparam name="form.finish" default="">

<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
var currentItem = 1;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td class="Copy" valign="top">Item Name:</td><td><input type="text" name="Itemname'+currentItem+'" id="Itemname'+currentItem+'" required="yes" message="Please enter a Name" class="TextBlock"></td></tr><tr><td class="Copy" valign="top">Item Description:</td><td><input type="text" name="ItemDesc'+currentItem+'" id="ItemDesc'+currentItem+'" required="yes" message="Please enter a Description" class="TextBlock"></td></tr><tr><td class="Copy" valign="top">Quantity</td><td><input type="text" name="Quantity'+currentItem+'" id="Quantity'+currentItem+'" required="yes" message="Please enter a Quantity" class="TextBlock"></td></tr><tr><td class="Copy" valign="top">Product Code:</td><td><input type="text" name="Code'+currentItem+'" id="Code'+currentItem+'" required="yes" message="Please enter a Code" class="TextBlock"></td></tr><tr><td class="Copy" valign="top">Price:</td><td><input type="text" name="Price'+currentItem+'" id="Price'+currentItem+'" required="yes" message="Please enter a Price" class="TextBlock"></td></tr>';
  $('#data').append(strToAdd);

 });
 });

 //]]>
 </script>

<cfif form.finish eq "finish">
    <cflocation addtoken="no" url="inv_new.cfm">
</cfif>
<form id="formID" method="post" action="inv_new.cfm" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="1" width="100%" class="Border" align="center">
<tr>
    <td class="CopyWhite" align="left" background="images/tab_bg.gif" colspan="2">Add a new invoice</td>
</tr>

<cfoutput>
    <cfif form.step eq 1>
            <cfquery name="get_vendors" datasource="#application.db#">
                SELECT * FROM tblpassVendor
                WHERE vendorActive = 1
                AND CompanyID = #session.companyid#
                AND VendorID IN (#GetAdmin.AdminVendors#)
            </cfquery>
            <tr>
                <td class="Copy" valign="top">Invoice Using:</td>
                <td>
                    <Select name="INVBY">
                        <cfloop query="get_vendors">
                            <option value="#vendorid#">#vendorname#</option>
                        </cfloop>
                    </Select>
                </td>
            </tr>
            <tr>
                <td class="Copy" align="center" colspan="2">
                <input type="Submit" name="Submit" value="Submit" class="submitbutton">
                <input type="hidden" name="step" value="2" />
                <input type="Reset" name="Reset" value="Reset" class="submitbutton">
                </td>
            </tr>
    </cfif>
    <cfif form.step eq 2>
            <input type="hidden" name="vendor" value="#FORM.INVBY#" />
            <tr>
                <td class="Copy" valign="top">Order Details:</td>
                <td>
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Payment Due:</td>
                <td>
                    <input type="text" id="date1" class="validate[required]" name="INVPAYMENTDATE" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Reference Number:</td>
                <td>
                    <input type="text" class="validate[required]" name="REFNUM" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Invoice Details:</td>
                <td>
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Pay Using Masterpass:</td>
                <td style="color:##000">
                    <input type="radio" name="MASTERPASS" value="1" checked="yes"> Yes<br />
                    <input type="radio" name="MASTERPASS" value="0" checked="no"> No<br />
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Pay Using Iveri:</td>
                <td style="color:##000">
                    <input type="radio" name="IVERI" value="1" checked="yes"> Yes<br />
                    <input type="radio" name="IVERI" value="0" checked="no"> No<br />
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Pay Using Other:</td>
                <td style="color:##000">
                    <input type="radio" name="OTHER" value="1" checked="yes"> Yes<br />
                    <input type="radio" name="OTHER" value="0" checked="no"> No<br />
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Billing Details:</td>
                <td>
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Name:</td>
                <td>
                    <input type="text" class="validate[required]" name="NAME" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Surname:</td>
                <td>
                    <input type="text" class="validate[required]" name="LNAME" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Tel:</td>
                <td>
                    <input type="text" class="validate[required]" name="TEL" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Email:</td>
                <td>
                    <input type="text" class="validate[required,custom[email]]" name="EMAIL" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Address line 1:</td>
                <td>
                    <input type="text" class="validate[required]" name="ADDR1" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Address line 2:</td>
                <td>
                    <input type="text" class="validate[required]" name="ADDR2" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">City/Town:</td>
                <td>
                    <input type="text" class="validate[required]" name="CITY" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Province/Region:</td>
                <td>
                    <input type="text" class="validate[required]" name="REGION" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Country:</td>
                <td>
                    <input type="text" class="validate[required]" name="COUNTRY" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" valign="top">ZIP/Postal Code:</td>
                <td>
                    <input type="text" class="validate[required]" name="ZIP" class="TextBlock">
                </td>
            </tr>
            <tr>
                <td class="Copy" align="center" colspan="2">
                <input type="Submit" name="Submit" value="Submit" class="submitbutton">
                <input type="hidden" name="step" value="3" />
                <input type="Reset" name="Reset" value="Reset" class="submitbutton">
                </td>
            </tr>
    </cfif>
    <cfif form.step eq 3>
        <cftry>
            <cfset payuid = "#CreateUUID()#">
            <cfset invuid = "#CreateUUID()#">
            <!---WRITE DATA TO TBLINV--->
            <cfquery name="write_inv" datasource="#application.db#" result="test">
                INSERT INTO tblpassInv
                (INVVENDORID,INVREF,INVNAME,INVLNAME,INVTEL,INVEMAIL,INVADDR1,INVADDR2,INVCITY,INVREGION,INVCOUNTRY,INVZIP,INVCOMPANY,INVCREATEDBY,INVDATECREATED,INVACTIVE,INVSEND,paymentuid,invuid,invmasterpass,inviveri,invother,invpaymentdate)
                VALUES
                (#form.vendor#,'#form.REFNUM#','#form.NAME#','#form.LNAME#','#form.TEL#','#form.EMAIL#','#form.ADDR1#','#form.ADDR2#','#form.CITY#','#form.REGION#','#form.COUNTRY#','#form.ZIP#',#session.companyid#,#session.adminid#,#createodbcdatetime(now())#,1,0,'#payuid#','#invuid#',#form.MASTERPASS#,#form.IVERI#,#form.OTHER#,'#FORM.INVPAYMENTDATE#')
            </cfquery>
            <cfset NewPrimaryKey = test.GENERATED_KEY>
            <!---<cfinclude template="inv_amend_prods_new.cfm">--->
            <tr>
            <td>
            <table  id="data">
                <tr>
                    <td class="Copy" valign="top">Products/Line Items:</td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="Copy" valign="top">Item Name:</td>
                    <td>
                        <input type="text" class="validate[required]" name="Itemname1" id="Itemname1" class="TextBlock">
                    </td>
                </tr>
                <tr>
                    <td class="Copy" valign="top">Item Description:</td>
                    <td>
                        <input type="text" class="validate[required]" name="ItemDesc1" id="ItemDesc1" class="TextBlock">
                    </td>
                </tr>
                <tr>
                    <td class="Copy" valign="top">Quantity</td>
                    <td>
                        <input type="text" class="validate[required,custom[number]]" name="Quantity1" id="Quantity1" class="TextBlock">
                    </td>
                </tr>
                <tr>
                    <td class="Copy" valign="top">Product Code:</td>
                    <td><input type="text" class="validate[required]" name="Code1" id="Code1" class="TextBlock"></td>
                </tr>
                <tr>
                    <td class="Copy" valign="top">Price: (eg. 100.00)</td>
                    <td><input type="text" class="validate[required,custom[number]]" name="Price1" id="Price1" class="TextBlock"></td>
                </tr>
                </table>
                </td>
                </tr>
                <tr>
                    <td>
                    <input type="Submit" name="Submit" value="Submit" class="submitbutton">
                    <input type="button" id="addnew" name="addnew" value="Add new item" />
                    <input type="hidden" id="items" name="items" value="1" />
                    <input type="hidden" name="step" value="4" />
                    <input type="hidden" name="invid" value="#NewPrimaryKey#" />
                    </td>
                </tr>
            <cfcatch type="any">
                <script type="text/javascript">
                    alert("An error has occured! Please try again later")
                </script>
            </cfcatch>
            </cftry>
    </cfif>
    <cfif form.step eq 4> 
        <cftry>       
            <cfloop from="1" to="#FORM.items#" index="i">
                <cfset thisname = form["Itemname" & i]>
                <cfset thisdesc = form["ItemDesc" & i]>
                <cfset thisqty = form["Quantity" & i]>
                <cfset thiscode = form["Code" & i]>
                <cfset thisprice = form["Price" & i]>
                <cfset thisprice = replace("#thisprice#",".","","All")>
                <cfquery name="add_items" datasource="#application.db#">
                    INSERT INTO tblpassInvItems
                    (invid,itemactive,itemname,itemdesc,itemqty,itemcode,itemprice)
                    VALUES
                        (#form.invid#,1,'#thisname#','#thisdesc#',#thisqty#,'#thiscode#','#thisprice#')
                </cfquery>
            </cfloop>

            <cfset invid = form.invid>
            <cfinclude template="inv_doc.cfm">
                <tr>
                    <td class="Copy" valign="top">Display all details for invoice:</td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="copy" valign="top">Download</td>
                    <td><a href="dynamicdocs/#this_filename#.pdf" target="_blank" title="Download">Download</a></td>
                </tr>
                <tr>
                    <td>
                    <input type="Submit" name="Submit" value="Submit" class="submitbutton">
                    <input type="hidden" id="items" name="items" value="1" />
                    <input type="hidden" name="step" value="5" />
                    <input type="hidden" name="invid" value="#form.invid#" />
                    </td>
                </tr>

            <!---<cfheader name="Content-Disposition" value="attachment;filename=#this_filename#.pdf">
            <cfcontent type="application/octet-stream" file="#expandPath('.')#/dynamicdocs/#this_filename#.pdf" deletefile="No">--->
        <cfcatch type="any">
            <script type="text/javascript">
                alert("An error has occured! Please try again later")
            </script>
        </cfcatch>
        </cftry>
    </cfif>
    <cfif form.step eq 5>
            <tr>
                <td class="Copy" valign="top" colspan="2">Please complete the following if you would like to send the invoice now or click on finish:</td>
            </tr>
            <tr>
                <td class="Copy" valign="top">Bcc:</td>
                <td>
                    <cfquery name="get_vendors" datasource="#application.db#">
                        SELECT * FROM tblpassVendor
                        WHERE vendorActive = 1
                        AND CompanyID = #session.companyid#
                        AND VendorID IN (#GetAdmin.AdminVendors#)
                    </cfquery>
                    <Select name="bcc">
                        <cfloop query="get_vendors">
                            <option value="#vendorbccemail#">#vendorbccemail#</option>
                        </cfloop>
                    </Select>
                </td>
            </tr>
            <tr>
                <td>
                <input type="Submit" name="Submit" value="Submit" class="submitbutton">
                <input type="hidden" id="items" name="items" value="1" />
                <input type="hidden" name="step" value="6" />
                <input type="hidden" name="invid" value="#form.invid#" />
                </td>
            </tr>
            <tr>
                <td><a href="inv_new.cfm" title="Finish">Save and Start New Invoice</a></td>
            </tr>
    </cfif>
    <cfif form.step eq 6>
        <cftry>
        <cfset invid = form.invid>
        <cfinclude template="inv_mail.cfm">
        <tr>
            <td class="Copy" valign="top" colspan="2">Thank you! Your Invoice has been sent!</td>
        </tr>
        <tr>
            <td class="copy" valign="top"></td>
            <td><a href="inv_new.cfm" title="Finish">Finish</a></td>
        </tr>
        <cfcatch type="any">
            <tr>
                <td class="Copy" valign="top" colspan="2">An Error has occured! Your invoice has not been sent</td>
            </tr>
        </cfcatch>
        </cftry>
    </cfif>
</cfoutput>
</table>
</form>
<cfinclude template="footer.cfm">

我没有使用像cfqueryparam这样的东西,因为它仍在开发中并且在安全的环境中

提前谢谢

3 个答案:

答案 0 :(得分:2)

我曾经做过这样的事情(很长一段时间使用它,代码可能会关闭,但更多的是一个想法):

<cfparam name="form.submitted" default=0>
<cfif structKeyExists(FORM,"submit") AND NOT FORM.submitted>
<!---action code goes here--->
<cfset form.submitted=1>
</cfif>
<form>
<!--- your other form elements--->
<input type="hidden" name="submitted" value="#FORM.submitted#">
</form>

我不确定,它可以通过多步骤形式帮助您,但通过在提交表单时设置标志,它有助于单步避免多次提交。

答案 1 :(得分:0)

您可以重定向到同一页面,这样可以避免重新发布表单。

答案 2 :(得分:0)

几天前我遇到过类似的情况。据我所知,最好的解决方案是,

  1. 重定向到其他某个页面,以防止重新提交表单。

  2. 如果重定向不合适,唯一的另一个选择是使用AJAX。