[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

How to pass a select option value and name

I have two "simple" CF pages. The first is an input form page where a user is given, among other things, the option of selecting 1 state from a select list. The second page is a processor page that verifies the data that was entered on the input page. I am trying to have the same state that the user selected on the input page, copied to the processor page so that the user can once again review, or change their selection, if necessary. I know how to do this using a database but the two pages I am working with do not need a database so I have a list of 50 option values as in:
                <option value="AK" >AK</option>
                <option value="AL" >AL</option>
in my select statement.

I thought I could use something like:
<select name="state">
              <option value="#form.state#"></option>
                <option value="AK" >AK</option>
</select>
But that doesn't display anything.

Might someone offer me some guidance?
Thanks,
Lee
0
lepirtle
Asked:
lepirtle
  • 5
  • 3
  • 2
  • +1
1 Solution
 
Andrew MaurerCommented:

<!--- First page --->
<form action="secondpage.cfm">
    <select name="selectedstate">
                  <option value="AL">AL</option>
                    <option value="AK" >AK</option>
    </select>
</form>
 
<!--- second page --->
<cfparam name="form.selectedstate" default="">
<form action="secondpage.cfm">
    <select name="state">
                  <option value="AL" <cfif form.selectedstate EQ "AL">selected</cfif> >AL</option>
                    <option value="AK"<cfif form.selectedstate EQ "AK">selected</cfif> >AK</option>
    </select>
</form>

Open in new window

0
 
black0psCommented:
The following is a custom tag that makes the select field selected.

<form name="myForm" action="myAction">
<select name="state">
              <option value="#form.state#"></option>
                <option value="AK" >AK</option>
</select>
<cf_selected select="myForm.state" value="form.state">
</form>
<!--- This file is named selected.cfm in the same directory it is being called from --->
<cfparam name="Attributes.Value" default="#Attributes.Select#">
<cfif NOT IsDefined("Caller." & Attributes.Value) AND IsDefined("Attributes.Value")>
	<cfset v = Attributes.Value>
<cfelseif IsDefined("Caller." & Attributes.Value)>
	<cfset v = Evaluate("Caller." & Attributes.Value)>
</cfif>
<cfif IsDefined("v")>
	<cfoutput>
	<script language="Javascript">
	<!--
	ox = '#v#'
	if (document.#Attributes.Select#.multiple == true) ox = ox.split(",");
	// loop through options in select
	for (i=0;i<=document.#Attributes.Select#.options.length; i++) {
		// if select is multiple
		if (document.#Attributes.Select#.multiple == true) {
			for (j=0;j<ox.length;j++) {
				if ((document.#Attributes.Select#.options[i]) && (document.#Attributes.Select#.options[i].value == ox[j] || document.#Attributes.Select#.options[i].text == ox[j])) {
					document.#Attributes.Select#.options[i].selected = true;
				}
			}
		}
		else {
			if ((document.#Attributes.Select#.options[i]) && (document.#Attributes.Select#.options[i].value == ox || document.#Attributes.Select#.options[i].text == ox)) {
				document.#Attributes.Select#.options[i].selected = true;
				break;
			}
		}
	}
	//-->
	</script>
	<cfif IsDefined("Attributes.Message")>
		<input type="hidden" name="#ListLast(Attributes.Select,".")#_required" value="#Attributes.Message#">
	</cfif>
	</cfoutput>
</cfif>

Open in new window

0
 
lepirtleAuthor Commented:
Zadoc,
Once again, thank you very much. You solve my questions faster than I can incorporate them into my pages to test. Amazing!!
I surely appreciate this and am ashamed to admit how many hours I wasted today trying to make these things work.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Andrew MaurerCommented:
Thats cool.
But what is the advantage in doing it that way?
0
 
Andrew MaurerCommented:
Did you mean to accept BlackOps Post? tahahha
0
 
lepirtleAuthor Commented:
Zadoc,
In my haste to give you the points, I didn't notice that someone else also volunteered their solution and I accidentally gave them credit for the answer. I don't know if their solution works, and I am sorry that I have not tested it, but I am emailing the Experts-Exchange CF forum leader to explain what happened and to give the points to you.

Thank you blackOps but Zadoc gave me the solution first and I, though I haven't tried your solution, I must give the points to Zadoc.
0
 
Andrew MaurerCommented:
Much appreciated lepirtle!
0
 
ZvonkoSystems architectCommented:
Meanwhile test my approach:
//First page:
 
<form action="secondpage.cfm" method="POST" preserveData="yes" >
    <select name="selectedstate" >
       <option value="AL">AL</option>
       <option value="AK" >AK</option>
       <option value="AJ" >AJ</option>
       <option value="AM" >AM</option>
    </select>
    <input type="submit" >
</form>
 
 
//secondpage.cfm:
<form name="myForm" action="secondpage.cfm" method="POST"  >
    <select name="selectedstate" >
       <option value="AL">AL</option>
       <option value="AK" >AK</option>
       <option value="AJ" >AJ</option>
       <option value="AM" >AM</option>
    </select>
    <script>document.myForm.selectedstate.value="<cfoutput>#FORM.selectedstate#</cfoutput>";</script>
    <input type="submit" >
</form>

Open in new window

0
 
ZvonkoSystems architectCommented:
Oh, the preserveData was only a test. You do not need it for select (or cfselect) because it does not work.
0
 
Andrew MaurerCommented:
Thanks again lepirtle.
0
 
lepirtleAuthor Commented:
You are welcome. I very much appreciate your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now