ColdFusion中的元素未定义错误。 cfparam不起作用

问题描述 投票:1回答:2

我遇到问题,我的ColdFusion代码返回“元素AUTHOR在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>
coldfusion coldfusion-10 cfml cfimport
2个回答
5
投票

错误告诉你出了什么问题。表单中没有author元素,或者根本没有表单范围。这是您发布的表单代码:

<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.contactnameform.commentform.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
投票

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

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

<cfset myVar = ReReplaceNoCase(#FORM.formfield#,"<[^>]*>","","ALL")/>
© www.soinside.com 2019 - 2024. All rights reserved.