Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Persisting Select Box Selection 3

Status
Not open for further replies.

hitechboy78737

Programmer
Nov 8, 2004
82
US
When I submit a form, how do I persist the item the user has selected?

How about when I read from the database... how do I select one of the options in a select box?

Do I need to make a javascript function?

I am an asp developer who has taken on a ColdFusion project... I'm sort of in the dark!

Kevin Howell
Briefcase of Talent- Austin, Texas
 
hitechboy78737, do you mean how do you pre-populate the user selected option in a select box?

If so:
Code:
<SELECT NAME="StateField">
  <cfquery name="getStates" datasource="#db#">
    Select * From States Order By State_ID
  </cfquery>
  
  <cfoutput query="getStates">
    <OPTION VALUE="#State_ID#" [COLOR=red]<cfif StateField is State_ID>Selected</cfif>[/color]>#StateName#</OPTION>
  </cfoutput>
</SELECT>



[sub]
____________________________________
Just Imagine.
[sub]
 
What do you mean 'persist'? Do you mean not allow the user to change the option? Can you be more specific?

____________________________________
Just Imagine.
 
Persist, persistance and data persistance are standard concepts and common web development terms. It means that when a form is posted to the server. the value the user had selected is "persisted", or stays selected when the page is refreshed.

In languages such as PHP, ASP Classic and all of the .NET flavors- data persistance is a fairly straight forward process, and with .net particularly it is automatic.

My problem is that I am adding additional functionality to an cold fusion application that I had no part in writing. This is my first crack at the CF language and though I am experienced in most web development languages, I have never worked with cold fusion. I don't seem to find the method CF uses to handle this common web task. It seems logical to me that this method would be present within the language framework and that CF and it's developers would use the same terms (eg. persist, data persistance) to describe core actions and functionality. Obviously I am wrong on that count and I apologise that I wasn't more specific to begin with.

In this example, there are several forms on a particular page. Each form (or section) is updated or posted back to the server individually with the page being refreshed (as opposed to going to another page). When form B is posted, the values from form A are lost when the page refreshes.

Thank you for your help!


Kevin Howell
Briefcase of Talent- Austin, Texas
 
ColdFusion persists values in the Session and Application scopes. In pure HTML behavior, nothing persists, as the objects on the page are based on the user request and are "new" for each request.

Unlike .NET, you have to roll your own persistence behavior in CF, whether through functions or by hard-coding storage/retrieval/evaluation of session-level variables. Check around; some enterprising individual may already have published a CFC or custom tag designed to do just what you're after.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
Eschewing obfuscation diurnally.
 
PERFECT! Thank you so much.

I suppose that the method I settled on using "hidden" fields is a valid way? We used to do this all the time in Classic ASP, I just assumed that there was a better way in CF than that.

Can you elaborate on "custom tags"? Are they like user built objects? Is there a good site that is dedicated to custom tags that you frequent?

Thanks for helping me with my "dirty" CF education!



Kevin Howell
Briefcase of Talent- Austin, Texas
 
hitechboy78737,
...he value the user had selected is "persisted", or stays selected when the page is refreshed

Which is the same "pre-populate the user selected option" as I mentioned above. Only difference was above I assumed you were retreiving info from a dB to populate the forms.

This would still work:
Code:
<cfoutput>
  <form name="testing" action="#cgi.SCRIPT_NAME#" method="post">
    <SELECT NAME="StateField">
      <option <cfif isdefined("FORM.StateField") and #FORM.StateField# is "Please Choose One">selected</cfif>>Please Choose One</option>
      <option <cfif isdefined("FORM.StateField") and #FORM.StateField# is "NY">selected</cfif> value="NY">NY</option>
      <option <cfif isdefined("FORM.StateField") and #FORM.StateField# is "TX">selected</cfif> value="TX">TX</option>
    </SELECT>
		
    <br/>
    <input type="text" size="25" maxlength="25" name="FirstName" <cfif isdefined("FORM.FirstName")>value="#FORM.FirstName#"</cfif>>
	
    <br/><br/>
    <input type="submit" name="submit" value="submit">
  </form>
</cfoutput>

The above, when page is refreshed, will look for the instance of the form variable and if exists populates those fields with the values the user selected.

As for custom tags, I go to: they have a good selection and its free.

____________________________________
Just Imagine.
 
WHat if the submit is triggered from another form? Does this still work?

I think the main problem with this particular project is that there is 5 forms on the page, each handling a "segment". The orgional designer set it up so each section could be updated seperately. So, if the user hasn't submitted the section I'm working on, but does submit from another section, the form that the select box resides just resets.

I coded around this by making the value of the select save in a hidden field that always gets picked up in the query string on any submit. I had it enter the value as the "selected" option tag of the select box. Only problem is that I wind up with a duplicate option.

Not graceful, but it works. I figured I must be missing something pretty obvious in the language, but appearently not.

Kevin Howell
Briefcase of Talent- Austin, Texas
 
CF is more like asp 3 than .net and php.

form variables only live for the life of the one form.
This makes sence.

as you know in .net you can only have 1 form per page. why would you move away from that?

The original creator of this app tried to control the flow of the form by splitting it up on the same page. nasty.

you have a few options,
1 keep each form on a new page and update the database as you go. you can display the old data to the user as you go but not in a form.

2 put all sections in 1 form.



We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
hitechboy78737, its still possible to populate the forms like I mentioned even in your situation, but the code would get messy. I think the best (and headache-free) solution would be TruthInSatire's where you split each section of the form on a new page.

What I usually do in those situations is on top of each page I indicate something like: Page 1 of X, this way the user knows how many sections of the forms there are. And, on each section have a 'back' button that user can click on to go back one page.

Let me know if you need any sample codes of that. I just did that for a multi-section from at my work last week.

____________________________________
Just Imagine.
 
While these are EXCELLENT solutions, for this particular project they are undoable. First and most importiant, the time and budget are not there to redesign the way the page works. Secondly, the client is resistant to any changes in the way the page works... they want fields added and thats it.

So, as you can see- I'm stuck cobbling onto a flawed architecture. I have hit on a solution, while not "right" its working, not breaking anything else and is what the client requested. Yes, you are right TruthInSatire... the resulting code was messy as hell and definatly not "how its done" it by any means. Just by virtue of the redundant trips to the server makes it "WRONG"! But whats 3 more trips when the thing is already going back and forth 10 times in the course of making a new order?

If anyone is interested, their site is bookaband.com The orgional developer, while inexperienced made a wonderful, feature rich application. Reguardless of the language and/or the coding I am very impressed with the features and business logic this guy put in. I can't show you the backend of course, but if you just looked at it in terms of features- I think even the most jaded developer would find it an impressive effort.

Thank you all so very much for your time and assistance. Tek-Tips and it's community is a wonderful resource and has never let me down!

Sincerely,




Kevin Howell
Briefcase of Talent- Austin, Texas
 
Reguardless of the language and/or the coding
there's nothing wrong with CF. In fact having been built on the J2EE platform you can do anything java can do with java objects. Custom tags and componants can also be created in native cf, java, and c++.

CF is designed to be easy to use and promote rapid delivery. its a little different paradigm than what you're used to, but that doesn't mean there is a flaw in the language. I've created entire control panels driving multiple websites using CF, there is nothing lacking in its ability. Remove your bias and you may like CF.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top