我遇到问题,我的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"> </div>
<div class="pat-bottomright"> </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"> </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"> </div>
</li>
</cfloop>
<!-- End Comment -->
</ul>
</div>
<h3>
Post Comment
</h3>
<div class="clr hline"> </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>
答案 0 :(得分: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个元素:contactname
,comment
,submit
和submitted
。这意味着在提交表单后,ColdFusion可以访问:form.contactname
,form.comment
,form.submit
和form.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?
答案 1 :(得分:0)
同意,未定义,因为它不存在于表单中。
绝对清理所有表单和网址数据。下面是一个例子:
<cfset myVar = ReReplaceNoCase(#FORM.formfield#,"<[^>]*>","","ALL")/>