1

我的 ColdFusion 代码返回“元素作者在 FORM 中未定义”时遇到问题。每当我提交表格时。我试过使用<cfparam>设置comment.author,但它也没有工作。我对 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>
4

2 回答 2

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个元素:contactnamecomment和。这意味着在提交表单后,ColdFusion 将可以访问 :、和。我假设您正在尝试将变量设置为表单字段。submitsubmittedform.contactnameform.commentform.submitform.submittedcomment.authorcontactname

您可以更改设置变量的代码,如下所示:

<cfset comment.author = form.contactname />

或者您可以更改定义表单字段的代码,如下所示:

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

无论哪种方式,对form范围的引用都必须与您在 HTML 表单中提供的名称相匹配。对于它的价值,您始终可以form在提交范围后转储范围以查看可用的内容,如下所示:

<cfdump var="#form#">

还要记住清理您从客户端收到的所有数据。

如何清理用户输入但保留 <pre> 标签的内容?

于 2013-06-12T16:45:21.877 回答
0

同意,未定义,因为它不存在于表单中。

并且绝对清理所有表单和网址数据。下面的一个例子:

<cfset myVar = ReReplaceNoCase(#FORM.formfield#,"<[^>]*>","","ALL")/>
于 2013-06-12T17:47:41.290 回答