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

Web forms introduction

Here, we will discuss web forms along with CFCs.

Let us view our web form page. Web forms are the same in ColdFusion as they are in any other HTML scenario. You might even note that there is very little use for web forms until you have a server-side technology such as ColdFusion. This is because when the form is posted, you need some sort of program to handle the data posted back to the server.

<!--- Example: 3_1.cfm --->
<!--- Processing --->
<!--- Content --->
<form action="3_1.cfm" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="idName" value="" /></td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" name="description" id="idDescription" value="" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" id="idPrice" value="" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>

First, notice that all of the information on the page is in the content section. Anything that goes from the server to the browser is considered as content. You can fill in and submit the form, and you will observe that all of the form fields get cleared out. This is because this form posts back to the same page. Self-posting forms are a valid method of handling page flow on websites. The reason why nothing seems to be happening is because the server is not doing anything with the data being sent back from the browser. Let us now add<cfdump var="#form#"/> to the bottom of the content, below the form tag, and observe what we get when we post the form:

Now we see another common structure in ColdFusion. It is known as the form structure. There are two types of common structures that send data to the server. The first one is called get and the second one is called post. If you see the code, you will notice that the method of the form is post. The form post setting is the same as coding with the form variable in ColdFusion.

You should also observe that there is one extra field in the form structure that is not shown in the URL structure variable. It is the FIELDNAMES variable. It returns a simple list of the field names that were returned with the form. Let us edit the code and change the form tag attribute to get. Then, refresh the page and click on the submit button:

From the previous screenshot, it is evident that the browser looks at the get or post value of the form, and sends a get or post back to the server. Post is a "form method" belonging to the past and this is why ColdFusion translates posted variables to the form structure. Now change the dump tag to "URL" and observe the results. Fill out the form and submit it again with the new change. This displays the values in our structure as we would expect. This means you can either send URL-type data back to the server, or form-type data with forms.

The advantage of sending form data is that form data can handle a larger volume of data being sent back to the server as compared to a get or URL request. Also, it is worth noting that this style of return prevents the form field values from being exposed in the URL. They can still be accessed, but are just not visible in the URL any more. So the method of choice for forms is post. Change both the method of the form attribute and the value of the cfdump var to form again.

The Description box is not ideal for entering product descriptions. So, we are going to use a text area in its place. Use the following code to accommodate a text area box. You can change the size of form's objects using attributes and styles:

<tr>
<td>Description:</td>
<td>
<textArea name="description" id="idDescription"></textArea> 
</td>
</tr>

Here, we see our form looking different. If you fill up the description with more content than the box can hold, it shows the scroll bars appropriately.

主站蜘蛛池模板: 宜兰县| 邓州市| 信丰县| 神木县| 滨州市| 江达县| 宿州市| 新泰市| 吕梁市| 卢龙县| 台东县| 宝坻区| 舟曲县| 金乡县| 什邡市| 交口县| 宜黄县| 云南省| 德化县| 田阳县| 张掖市| 宝应县| 新源县| 双鸭山市| 石棉县| 漳平市| 深泽县| 益阳市| 平原县| 长宁区| 平顶山市| 泽州县| 绥滨县| 黑山县| 青冈县| 巴楚县| 泸西县| 崇礼县| 灌南县| 玛曲县| 廊坊市|