Copy link to clipboard
Copied
I'm building a form for my office that submits info to a database. I'd like to use the same form to add new info and edit existing info. I also want the form to retain data once it's sumitted (i.e. if the user clicks submit and there's a blank field required, the form is re-loaded with the data retained.
I am not allowed to use javascript (agency rule).
I started off with something like this for each field:
<cfinput type="text" name="title" value="#FORM.title#" />
This is fine, but then I added the edit option. A user pics an entry on a preceding page that passes an id number in the URL (ex: form.cfm?edit=32) to the form.
When URL.edit has a value of other than zero (the assigned cfparam default), it triggers a query that grabs the data out of the database. I want this data to populate the form, so I tried this:
<cfinput type="text" name="title" value="#iif(URL.edit eq 0,DE(FORM.title),DE(queryname.title))#" />
This works perfectly if the query is run, but I get an error when I load the blank form:
Element TITLE is undefined in QUERYNAME.
I'm basically trying to make the default form value blank if the form is loaded from scratch, make it the previous form values if the form is re-loaded after submitting, or and make it use the query if a URL param is passed.
Is there any way to make this work? I don't want to have to write multiple versions of the form.
1 Correct answer
Your half way there, you just basiclly need to combine your two approaches.
Set a collection of variables to populate the form fields.
Then IF there is a query run, set that collection to values from the query. It would look roughly like this, and there are many different ways to skin this for different framework apporached.
<cfparam name="title" default="">
<cfif query ID exists>
<cfquery.....>
<cfset title = query.title>
</cfif>
<cfinput type="text" name="title" value="#title#">
Copy link to clipboard
Copied
Your half way there, you just basiclly need to combine your two approaches.
Set a collection of variables to populate the form fields.
Then IF there is a query run, set that collection to values from the query. It would look roughly like this, and there are many different ways to skin this for different framework apporached.
<cfparam name="title" default="">
<cfif query ID exists>
<cfquery.....>
<cfset title = query.title>
</cfif>
<cfinput type="text" name="title" value="#title#">
Copy link to clipboard
Copied
@Ian: You, sir, are a genius! Thank you very much. I suppose it should have been obvious, but it wasn't to me! The form works perfectly now.
@Dan: Good catch! The actual rule says that a page must still work with JS disabled and lose no functionality. I think the only thing the JS in the cfinputs does is validation, which I'm doing by hand post-submit anyway, so the form should still work with JS disabled.
The one thing that I do find maddening is that when I submit a cfform, it auto-scrolls down to the first input, so the top of the screen (where I display messages) isn't seen until the user scrolls up. I suppose I could solve that by putting a hidden cfinput at the top of the page, but it's still annoying to have to do that.
Copy link to clipboard
Copied
If your page is autoscrolling, you are doing something to make that happen, probably with javascript, the forbidden language. Do you have anything going on that sets your focus to the first form field?
Copy link to clipboard
Copied
There is no initial focus on any of the form fields. I've solved the problem with adding a #content to each link (our office standards dictate a named anchor called "content" at the start of the page content).
Oddly, it's only this one form that does it. I have two other forms that are part of this app, and neither scroll. One doesn't count I suppose, because it uses <form> rather than <cfform>
Copy link to clipboard
Copied
First, cfform format="html" is based on javascript so using it is against your agency rule. Hopefully you are using a flash form.
Next, whether you are adding or editing seems to be based on a url variable. That being the case, on your form page, if you have the variable, run the query and get the values and set them to variables. Otherwise set those variables to empty strings. Make sure to add a hidden field for the record id.
On your action page, use that hidden field to determine whether you are updating or inserting. Once the sql is done, send the user back to the form page, and include a url variable for the record you either updated or inserted.

