2013-06-12 88 views
1

我遇到了我的ColdFusion代碼返回「元素AUTHOR在FORM中未定義」的問題。每當我提交我的表格。我試過用<cfparam>來設置comment.author,但它也沒有工作。我對ColdFusion相當陌生,所以任何推理評論都會很棒!ColdFusion中的元素未定義錯誤

<cfparam name="form.submitted" default="0" /> 
<cfset blogPost = EntityLoad('BlogPost',url.id,true) /> 
<cfif form.submitted> 
    <cfset comment = EntityNew('BlogComment') /> 
    <cfset comment.author = form.author /> 
    <cfset comment.comment = form.comment /> 
    <cfset comment.createdDateTime = now() /> 
    <cfset blogPost.addComment(comment) /> 
    <cfset EntitySave(blogPost) /> 
</cfif> 

<cfimport taglib="customTags/" prefix="layout" /> 
<layout:page section="blog">  

     <!-- Content Start --> 

     <!--Card --> 
     <div id="content"> 
      <div class="card-pattern"> 
       <!-- blog --> 
       <div id="blog"> 
        <div class="clr"> 
         <div class="top-bg1"> 
          <div class="top-left"> 
           <div><h1>Blog</h1></div> 
          </div> 
         </div> 
         <div class="clr"> 
          <div class="pat-bottomleft">&nbsp;</div> 
          <div class="pat-bottomright">&nbsp;</div> 
         </div> 
        </div> 
        <div class="blog-top"> 
         <div class="clr"> 
         <cfoutput> 
          <div class="left"> 
           <!-- Blog Title --> 
           <h2 class="big"> 
            #blogPost.title# 
           </h2> 
           <!-- Date Published --> 
           <h5> 
            <strong>Date Posted</strong>: #dateformat(blogPost.dateposted,'mm/dd/yyyy')# 
           </h5> 
           <!-- Blog Body --> 
            #blogPost.body# 
           <!-- Blog Export --> 
           <p> 
            <a href="exportToPDF.html?id=#blogPost.id#" target="_new"><img src="assets/images/export_pdf.png" border="0"/></a> 
           </p> 
           <!-- Blog Comments Section --> 
           <h3> 
            Comments #arrayLen(blogPost.getComments())# 
           </h3> 
           <div class="clr hline">&nbsp;</div> 

           <div class="clr comments"> 
            <ul> 
             <!-- Start Comment --> 
             <cfloop array="#blogPost.getComments()#" index="comment"> 
             <li> 
              <p> 
               <strong>Posted On:</strong> #dateFormat(comment.createdDateTime,'mm/dd/yyyy')# at #timeformat(comment.createdDateTime,'short')# By #comment.author# 
              </p> 
              <p> 
               #comment.comment# 
              </p> 
              <div class="clr hline">&nbsp;</div> 
             </li> 
             </cfloop> 
             <!-- End Comment --> 
            </ul> 
           </div> 
           <h3> 
            Post Comment 
           </h3> 
           <div class="clr hline">&nbsp;</div> 

           <div class="clr postComment"> 
            <form action="BlogPost.cfm?id=#blogPost.id#" method="post" id="form"> 
             <div> 
              <label>Name <span class="font-11">(required)</span></label> 
              <input name="contactname" type="text" class="required" /> 
             </div> 
             <div class="textarea"> 
              <label>Comment <span class="font-11">(required)</span></label>    
              <textarea name="comment" rows="6" cols="60" class="required"></textarea>   
             </div> 
             <div> 
              <input id="submitBtn" value="Submit" name="submit" type="submit" class="submitBtn" /> 
             </div> 
             <input type="hidden" name="submitted" value="1" /> 
            </form> 
           </div> 
          </div> 
         </cfoutput> 
          <div class="right" > 
           <h2>Categories</h2> 
           <!-- Blog Specific Categories --> 
           <div id="categories" align="center"> 
            <ul> 
             <li><a href="#">ColdFusion</a></li> 
             <li><a href="#">Development</a></li> 
            </ul> 
           </div> 
          </div> 
         </div> 
        </div> 
        <div class="clr"></div> 
       </div> <!--blog end --> 

</layout:page> 

回答

5

錯誤是告訴你什麼是錯的。在您的表單中沒有作者元素或根本沒有表格範圍。這裏是你貼在表格代碼:

<form action="BlogPost.cfm?id=#blogPost.id#" method="post" id="form"> 
    <div> 
     <label>Name <span class="font-11">(required)</span></label> 
     <input name="contactname" type="text" class="required" /> 
    </div> 
    <div class="textarea"> 
     <label>Comment <span class="font-11">(required)</span></label>    
     <textarea name="comment" rows="6" cols="60" class="required"></textarea>   
    </div> 
    <div> 
     <input id="submitBtn" value="Submit" name="submit" type="submit" class="submitBtn" /> 
    </div> 
    <input type="hidden" name="submitted" value="1" /> 
</form> 

只包含4個元素:contactnamecommentsubmitsubmitted。這意味着在提交表單後,ColdFusion將可以訪問:form.contactname,form.comment,form.submitform.submitted。我假定你正試圖將你的comment.author變量設置爲contactname表單字段。

你既可以改變你的代碼,你在哪裏設置變量,就像這樣:

<cfset comment.author = form.contactname /> 

或者你可以改變你的代碼中的表單字段的定義,像這樣:

<input name="author" type="text" class="required" /> 

無論哪種方式,對form範圍的引用都必須與您在HTML表單中提供的名稱相匹配。對於它的價值,你可以隨時轉儲form範圍內,可以提交後看到什麼是可用的,就像這樣:

<cfdump var="#form#"> 

還記得消毒您從客戶端接收的所有數據。

How can I sanitize user input but keep the content of <pre> tags?

+0

對於此特定類型的錯誤消息,此答案的第一段並不總是正確的。有時這意味着根本沒有形式範圍。 –

+0

我會更新該段落。 –

+0

真棒,感謝您的詳細解釋! –

0

同意,不確定,因爲它不存在於形式存在。

絕對清理所有表單和網址數據。下面的一個示例:

<cfset myVar = ReReplaceNoCase(#FORM.formfield#,"<[^>]*>","","ALL")/>