|
Cool Forms with ColdFusion
Page 7
Feeling Robust?
This brings us to processing. Once we've filtered through all the bad data and we've finally got this dude to fill in his name correctly, we have to process the submission. This is done in two simple steps. First, we subtract a book from the inventory table. Then we enter the order into the orders table. To do this, open up your process.cfm file and insert the following code at the top (this should be pretty easy to follow): |
<cfquery name="qInsert" datasource="wired_cf">insert into orders(name, address, city, state, zip, phone, item, uid)values('#form.name#', '#form.address#', '#form.city#', '#form.state#', #form.zip#, '#form.phone#', #form.choice#, #form.rand#)</cfquery> <cfquery name="qUpdate" datasource="wired_cf"> update inventory set instock = instock - 1 where id = #form.choice#</cfquery> Every time we get a valid submission, it's recorded in the database. So we're done, right? Not quite. We want to make this form robust so it won't break. And there is a lot of stuff that might break our form. There's no guarantee that things will always run perfectly. But you can reduce the chances of mishaps by finding all the weak spots and adding some reinforcement to them. In order to do this, we're going to play with the <CFTRY> and <CFCATCH> tags. You can put <CFTRY> tags around any code that might generate an error. If anything goes wrong, the server looks at the <CFCATCH> tag to see what to do. Since this exercise is merely for the purposes of demonstration, I'm going to put warning messages only in the <CFCATCH> blocks, but I could put any sort of routine in this tag. There are three places where we are going to use these tags. First, we're going to surround everything in form.cfm in a try-catch block. Iin essence, this covers our entire application in case something goes wrong. Next, we want to surround all of our queries, ensuring that we can catch database errors. Finally, we want to surround each of our include statements just in case any of the files get moved without our knowledge. If you go through and add these, you'll have pretty good protection from errors, making the application less susceptible to breaking. But we still need to protect against accidents. If you have spent any amount of time working with forms, you know that users get impatient. If the form doesn't go through immediately, users will sometimes pound their Submit buttons until they see results. Or if they think they've made a mistake, they will hit their Back buttons, make changes, and then submit again, duplicating their submissions. These multiple submissions can be a royal pain. To avoid them, we are going to add a hidden field to our input.cfm file. It looks like this: |
<cfoutput><input type="hidden" value="#Rand()#" name="rand"></cfoutput> In my database I have a field called "uid." This field was put there to store a unique identifier. That identifier is the random number created by the line of code above. How does a random number help us prevent multiple submits? If I told you, I'd have to kill you - not to keep the secret but for the protection of the gene pool. Needless to say, if we check for this number and find a match, it is a safe bet that this form has been submitted before. We perform the check like this: |
<cfquery name="qUnique" datasource="#webmonkey#"> select id from orders where uid = #form.rand#</cfquery> Add that to the top of your process.cfm file, and you are almost home free. Don't forget to include the try-catch blocks. We've done a lot with this form, but there's still one more thing left to do. We're going to make it adaptable. |
next page»
|
|
|