官术网_书友最值得收藏!

Improving page flow

The page was refreshed and showed us an update. But it might not be obvious that an update was made. There is also an issue that it does not appear as if the data was updated because we pulled the data object details before the update was made. Also when we saved our edit page data, it would be nice if we were sent back to the list page. Let us take a look at the redirect tag in ColdFusion,<cflocation>. After an insert or update, the browser will be returned back to the list page. We will also pass a message to the page so that the user will see the updates. First, we will create a message in each of our insert and action code segments. Then, we will push the page back to the list page by passing the message with it:

<cfif structKeyExists(attributes,"submit")>
<cfif attributes.id EQ 0>
<cfquery datasource="cfb" name="qryInsert">
INSERT INTO product( name , description , price ) VALUES( <cfqueryparam value="#attributes.name#"> , <cfqueryparam value="#attributes.description#"> , <cfqueryparam value="#attributes.price#"> )
</cfquery>
<cfset returnMessage = "Your product has been added.">
<cfelse>
<cfquery datasource="cfb" name="qryUpdate">
UPDATE product SET name = <cfqueryparam value="#attributes. name#"> , description = <cfqueryparam value="#attributes. description#"> , price = <cfqueryparam value="#attributes.price#"> WHERE ID = <cfqueryparam value="#attributes.id#">
</cfquery>
<cfset returnMessage = "Your product (#attributes.name#) has been updated.">
</cfif>
<cflocation url="product_list.cfm?message=#returnMessage#">
</cfif>

You can see that we tacked the message to the end location of our target page. We need to add a<cfparam> tag to the calling page, so it will be able to handle calls where the URL variable is not available. Then we can add a little section of the page to show the messages, when they exist. Let us edit the product_list.cfm page as follows:

<!--- Example: product_list.cfm --->
<!--- Processing --->
<cfparam name="url.message" default="">
<cfscript>
objProduct = createObject("component","product").init(dsn="cfb");
rsProducts = objProduct.getRecordset();
</cfscript>
<!--- Content --->
<cfif url.message NEQ "">
<div>
<cfoutput>#url.message#</cfoutput>
<hr />
</div>
</cfif>
<h3>Select a product to edit.</h3>
<ul>
<cfoutput query="rsProducts">
<li>
<a href="product_edit.cfm?id=#rsProducts.id#">#rsProducts.name#
</li>
</cfoutput>
</ul>

This is how the page looks, when we submit our form:

主站蜘蛛池模板: 澄迈县| 延吉市| 清水河县| 千阳县| 长汀县| 青神县| 启东市| 南开区| 赤壁市| 灌云县| 任丘市| 阆中市| 南汇区| 普宁市| 阳原县| 武汉市| 郸城县| 若尔盖县| 黄大仙区| 张家港市| 五莲县| 海安县| 吉林市| 焦作市| 林口县| 兴文县| 紫云| 夏津县| 始兴县| 木里| 乐清市| 乌兰县| 徐州市| 马龙县| 棋牌| 合水县| 虎林市| 太保市| 浏阳市| 墨江| 内丘县|