1

当我尝试删除购物车中的商品时。我收到“表单中未定义元素 ID”错误。

我哪里错了?

我正在使用 MSSQL 2008 r2,Coldfusion 10。

概括:

ticket.cfm 这是显示产品的页面,还包含一个带有隐藏值的表单,要传递给 cart_manage.cfm。

cart_manage.cfm 是 ticket.cfm 和 cartlist.cfm 的操​​作页面

cartlist.cfm 是一个显示购物车内容的页面。

application.cfm 会话变量。

应用程序.cfm

<cfapplication sessionmanagement="yes">
<cfapplication name="cart" clientmanagement="Yes"
sessionmanagement="Yes"
sessiontimeout="#CreateTimeSpan(0,0,15,0)#"
applicationtimeout="#CreateTimeSpan(0,2,0,0)#">

<cfparam name="session.allowin" default="false">

门票.cfm

    <cfif NOT IsDefined('SESSION.cart')>
  <cfset SESSION.cart = ArrayNew(1) />
</cfif>
<cfquery datasource="sqltest" name="getTickets">
select *,
       CASE WHEN Friday=1and Saturday=1and Sunday=1
            THEN 'All three days'
            WHEN Friday=0and Saturday=0and Sunday=0
            THEN 'None'
            ELSE
       STUFF(
       case when Friday=1 then ',Friday' else '' end
     + case when Saturday=1 then ',Saturday' else '' end
     + case when Sunday=1 then ',Sunday' else '' end, 1,1,'')
            END WhichDays
from tickets_performances;
</cfquery>

<table width="600" border="0">
  <tr>
    <td>Day</td>
    <td>Price</td>
    <td>How Many Left</td>
    <td>Quantity</td>
  </tr>
  <p> You can only purchase a maximum of two tickets at a time. Having a ticket limit ensures fairness to all those buying tickets. The ticket limit applies per account, billing address, and/or credit card. Please observe the ticket limit as over purchases may be cancelled without notice or warning. </p>
      <cfform action="cart_manage.cfm" name="form" method="post">

  <cfoutput query="getTickets">
      <tr>
        <td>#WhichDays#</td>
        <td>&pound;#price#</td>
        <td>#stock#</td>
        <td><cfinput type="text" id="quantity" name="quantity" size="5" class="field" maxlength="1" value="0"/></td>
        <td><cfinput type="hidden" name="id" value="#getTickets.ticket_performanceID#" />
          <cfinput type="hidden" name="item" value="#getTickets.WhichDays#" />
          <cfinput type="hidden" name="price" value="#getTickets.price#" />
          <cfinput type="submit" name="add_button" value="Add to Cart"></td>
      </tr>
  </cfoutput>
      </cfform>

</table>

购物车管理.cfm

<cfset newitem = 0>
<cfloop from="1" to="#arrayLen(session.cart)#" index="i">
<cfif session.cart[i].itemid EQ #form.id#>
<cfset session.cart[i].quantity = session.cart[i].quantity + #form.quantity#>
<cfset newitem = 1>
<cfbreak>
</cfif>
</cfloop>

<cfif newitem EQ 0>
<cfset temp = arrayAppend(session.cart, structNew())>
<cfset session.cart[arrayLen(session.cart)].itemid = #form.id#>
<cfset session.cart[arrayLen(session.cart)].item = #form.item#>
<cfset session.cart[arrayLen(session.cart)].quantity = #form.quantity#>
<cfset session.cart[arrayLen(session.cart)].price = #form.price#>
<cflocation url="cartlist.cfm">

</cfif>

<cfif IsDefined('FORM.delete_button.y')>
  <cfloop from="#ListLen(FORM.delete_index)#" to="1" index="i" step="-1">
    <cfset ArrayDeleteAt(SESSION.cart, ListGetAt(FORM.delete_index, i))>
  </cfloop>
  <cflocation url="cartlist.cfm">
<cfelseif IsDefined('FORM.update_button.y')>
  <cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i">
    <cfset SESSION.cart[i].quantity = FORM["quantity_" & i] >
  </cfloop>
  <cflocation url="cartlist.cfm">
<cfelseif IsDefined('FORM.checkout_button.y')>
  <cflocation url="checkout.cfm">
</cfif>
  <cflocation url="cartlist.cfm">

购物车清单.cfm

<cfset nTotal = 0 />
<cfform action="cart_manage.cfm" method="post">
  <table width="100%">
    <tr valign="top">
      <td>
        <table width="100%" class="white">
          <tr>
            <td class="tblehead">&nbsp;</td>
            <td class="tblehead">Item</td>
            <td class="tblehead">Price Per Item</td>
            <td class="tblehead">Quantity</td> 
            <td class="tblehead">Price</td>
          </tr>
          <cfoutput>
            <cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i">
              <tr>
                <td height="40" width="40" align="center" class="dkturq">
                  <cfinput type="checkbox" name="delete_index" value="#i#" />
                </td>
                <td height="40" class="dkturq">
                  #SESSION.cart[i].item#
                </td>
                <td height="40" class="dkturq">
                  &pound;#(SESSION.cart[i].price)#
                </td>
                <td height="40" class="dkturq">
                  <cfinput type="text" name="quantity_#i#" value="#SESSION.cart[i].quantity#" size="5" class="field" />
                </td>
                <td height="40" class="dkturq">
                  <cfset nPrice = SESSION.cart[i].quantity * SESSION.cart[i].price />
                  <cfset nTotal = nTotal + nPrice />
                  &pound;#(nPrice)#
                </td>
              </tr>
            </cfloop>
          </cfoutput>
        </table>
      </td>
      <td></td>
      <td>
        <table width="100%" height="100%" class="white">
          <tr>
            <td class="tblehead">
              Summary
            </td>
          </tr>
          <tr>
            <td class="dkturq">
              total:
              <cfoutput>&pound;#(nTotal)#</cfoutput>
              <br /><br /><br /><br />
              <a href="clear.cfm">Clear Shopping Cart</a>
                <cfinput type="submit" name="update_button" id="update_button" value="update"  />
               <cfinput type="submit" name="delete_button" id="delete_button" value="delete"  /><br /></a>

            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</cfform>

表格的CFDUMP

在此处输入图像描述

Session.cart 的 CFDUMP

在此处输入图像描述

错误: 在此处输入图像描述

当我按下删除时 CFDUMP 在此处输入图像描述

4

1 回答 1

3

感谢 Leigh,我设法让它工作。

(聊天总结..)

最初的问题与错误消息所说的差不多。即尝试使用不存在的表单域。原因是“tickets.cfm”和“cartList.cfm”包含不同的表单字段。“cartList.cfm”中的表单不包含名为 的字段form.id,因此提交该表单时会出错。为了避免错误,您需要在访问它之前验证它的form.id存在。structKeyExists()

但是,我们决定改为修改操作页面代码。此外,“cartList.cfm”表单已更改为使用itemID而不是index. 原因是,index可以更改可能导致错误的项目被删除或更新。仍有改进的余地,但这里是更改的要点:

* cartList.cfm (表单域) *

<!--- use itemID's instead of "index" in all form fields --->
<cfinput type="checkbox" name="delete_itemID" value="#SESSION.cart[i].itemid#" />
<cfinput type="text" name="quantity_#SESSION.cart[i].itemid#" value="#SESSION.cart[i].quantity#" size="5" class="field" /><br>

购物车管理.cfm

<!--- ADD item to cart ---->
<cfif structKeyExists(FORM, "add_button")>
    ... code to add items here ...

<!--- DELETE from cart ---->
<cfelseif structKeyExists(FORM, "delete_button")>
    <!--- 
        Ensure the field exists to prevent errors. Note: A more 
        efficient option is to test the field's existence in the cfelseif 
    --->
    <cfparam name="FORM.delete_itemID" default="">
    <cfloop from="#ArrayLen(SESSION.cart)#" to="1" index="i" step="-1">
        <!--- if this item was marked as "deleted", remove it --->
        <cfif listFind(FORM.delete_itemID, SESSION.cart[i].itemID)>
            <cfset arrayDeleteAt(SESSION.cart, i)>
        </cfif>
    </cfloop>

<!--- UPDATE item in cart ---->
<cfelseif structKeyExists(FORM, "update_button")>

    <cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i"> 
        <cfset currentItem = session.cart[i]>  
        <!--- Note: For safety, verify the field exists first --->
        <cfset currentItem.quantity = FORM["quantity_" & currentItem.itemID] >
    </cfloop>

</cfif>
于 2013-05-27T19:18:53.243 回答