2013-05-27 71 views
1

當我嘗試刪除購物車中的物品時。我收到「元素ID未在表單中定義」錯誤。Coldfusion/SQL:元素ID未在表單中定義

我哪裏錯了?

我使用的是MSSQL 2008 R2,ColdFusion的10

摘要:

tickets.cfm這是顯示產品的頁面,還包含隱藏的值的窗體上cart_manage傳遞.CFM。

cart_manage.cfm是兩個tickets.cfm和cartlist.cfm動作頁

cartlist.cfm是顯示購物車內容的頁面。

application.cfm會話變量。

application.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"> 

tickets.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> 

cart_manage.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"> 

cartlist.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> 

FORM的CFDUMP

enter image description here

Session.cart的CFDUMP

enter image description here

錯誤: enter image description here

CFDUMP當我按下刪除 enter image description here

+1

什麼頁面發生錯誤?你能發佈確切的錯誤信息嗎? –

+1

步驟1 - cfdump您的表單並驗證id元素是否存在。 –

+0

@ Dan當我添加表單的Cfdump時,我得到以下錯誤:元素ITEMID在作爲表達式的一部分引用的CFML結構中未定義。 –

回答

3

感謝利,我設法得到它的工作。

(摘要從聊天..)

原來的問題是相當多的錯誤消息,說的話。即嘗試使用不存在的表單域。原因是「tickets.cfm」和「cartList.cfm」包含不同的表單字段。 「cartList.cfm」中的表單不包含名爲form.id的字段,因此該表單在提交時出錯。爲避免該錯誤,您需要先驗證form.id是否存在,然後再訪問structKeyExists()

但是,我們決定修改操作頁面代碼。此外,「cartList.cfm」表格更改爲使用itemID而不是index。原因是,index可能會更改,這可能會導致錯誤的項目被刪除或更新。還有改進的餘地,但這裏的變化JIST:

* 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> 

cartManage.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> 
+1

(編輯)希望你不介意,但我更新了您的答案與所做更改的簡要摘要。所以其他人更容易找到答案。 (我也加了一些關於潛在改進的評論)。如果你不喜歡更改,請隨時編輯:) – Leigh

+0

謝謝你,李。 –