Link to home
Start Free TrialLog in
Avatar of Zac123
Zac123Flag for United Kingdom of Great Britain and Northern Ireland

asked on

pass variable in URL from selct menu

hi all,

i'm sure this is fairly simple but i cant work out how to do it.

i have a select menu with is populated by a query. i would like on submit of the select menu to pass the category_ID of whatever is selected in the URL to the action page.

looking at the code will probably make more sence.
this is the query:
 
<cfquery name="left_side_getCat" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
SELECT category_Name as catName, category_ID
FROM tbl_prdtcategories
ORDER BY catName ASC
</cfquery>
 
this is the select menu:
 
<form action="results.cfm" method="post" name="left_nav">
<select name="left_nav_cats" multiple>
<cfoutput query="left_side_getCat">
<option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
</cfoutput>
</select>
<input type="submit" name="button" id="button" value="Submit">
</form>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of erikTsomik
erikTsomik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SidFishes
from your code I can't see where a url applies but perhaps this is what you are looking for
<cfoutput>
 
<cfif structkeyexists(form,"left_nav_cats")>
	<cfset formX = form.left_nav_cats>
	<cfloop list="#formX#" index="x">
	 Value = #x#<br>
	</cfloop> 
</cfif>
 
 
<form action="index.cfm" method="post" name="left_nav">
<select name="left_nav_cats" multiple>
 
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
 
</select>
<input type="submit" name="button" id="button" value="Submit">
</form></cfoutput>

Open in new window

Avatar of Zac123

ASKER

erikTsomik,

your first option has no effect. the second option throws an error.

SidFishes,

i'm trying yours now

Avatar of Zac123

ASKER

right... to be honest Sid, i dont understand what you've gievn me there. allow me to explain what i'm trying to achive...

when a user click on submit i need the user to be taken to the results page. the url of the results page should look like this:

http://www.mydomain.com/results.cfm?category=999 

the 999 is obviously the bit that would chnage depending on the users selection from the select menu.

i had been trying things that look more like erics first option.

what error you get
Avatar of Zac123

ASKER


Invalid CFML construct found on line 35 at column 49.  
ColdFusion was looking at the following text:
left_nav_cats.

The CFML compiler was processing:

An expression that began on line 35, column 43.
The expression might be missing an ending #, for example, #expr instead of #expr#.
The tag attribute url, on line 35, column 15.
A cflocation tag beginning on line 35, column 4.
A cflocation tag beginning on line 35, column 4.
 
 
The error occurred in D:\inetpub\wwwroot\nextdaypro\wwwroot\test_site\includes\all_categories_links.cfm: line 35
 
33 :
34 : <cfif isDefined("form.button") and form.button eq "Submit">
35 :   <cflocation url="results.cfm?categoryID=#form left_nav_cats.#" addtoken="no">
36 : </cfif>
37 :
 
 

this is the code:
 
 
<form action="" method="post" name="left_nav">
<select name="left_nav_cats"  class="left_nav_select"multiple>
<cfoutput query="left_side_getCat">
<option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
</cfoutput>
</select>
<input type="submit" name="button" id="button" value="Submit">
</form>
 
<cfif isDefined("form.button") and form.button eq "Submit">
  <cflocation url="results.cfm?categoryID=#form left_nav_cats.#" addtoken="no">
</cfif>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
since i didn't know what you were trying to do with the url, i just provided a generic example

you'd basically just do this

<cfset myUrl= "http://www.mydomain.com/results.cfm?category=#x#">

however, since you have multiple values allowed how do you want to send those values back? As a list?

http://www.mydomain.com/results.cfm?category=2,3,5

<cfif structkeyexists(form,"left_nav_cats")>
<cfparam name="myURL" default="">
        <cfset formX = form.left_nav_cats>
        <cfloop list="#formX#" index="x">
          <cfset myUrl = MyUrl & #x#>
        </cfloop>


<cfset myUrl= "http://www.mydomain.com/results.cfm?category=#myUrl#">

</cfif>

<cflocation url="#myURL#">
Avatar of Zac123

ASKER

excellent, so this is now working. But Sid has a good point. what if there are multiple selections?

would it be best to use Sid's idea or is there a way to adapt this:
<form action="" method="post" name="left_nav">
<select name="left_nav_cats"  class="left_nav_select"multiple>
<cfoutput query="left_side_getCat">
<option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
</cfoutput>
</select>
<input type="submit" name="button" id="button" value="Submit">
</form>
 
<cfif isDefined("form.button") and form.button eq "Submit">
  <cflocation url="results.cfm?category=#form.left_nav_cats#&imagesonly=1" addtoken="no">
</cfif>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zac123

ASKER

ok so have inserted the code as above and replaced the <option value="1">1</option>s with <option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>

but how exactly do i get all the list/option items to disply? because currently there is only one displaying.
<cfoutput>
 
<cfif structkeyexists(url,"category")>
<cfloop list="#url.category#" index="x">
    You have chosen Category #x#<br>
</cfloop>
 
 
</cfif>
 
<cfif structkeyexists(form,"left_nav_cats")>
<cfparam name="myURL" default="">
        <cfset formX = form.left_nav_cats>
        <cfloop list="#formX#" index="x">
          <cfset myUrl = MyUrl & #x# & ",">
    </cfloop>
 
 
<cfset myUrl= "index.cfm?category=#left(myUrl, len(myURL)-1)#">
<cflocation url="#myURL#">
</cfif>
 
<form action="index.cfm" method="post" name="left_nav">
<select name="left_nav_cats"  class="left_nav_select" multiple>
  <option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
 
</select>
<input type="submit" name="button" id="button" value="Submit">
</form></cfoutput>

Open in new window

Change your form from POST to GET.  This passes all form variables in the URL

<form action="results.cfm" method="GET" name="left_nav">

Open in new window

just add back your original query loop

<cfoutput query="left_side_getCat">
<option ...


Avatar of Zac123

ASKER

erm... it didnt like that :-(

Invalid tag nesting configuration.  
A query driven cfoutput tag is nested inside a cfoutput tag that also has a query attribute. This is not allowed. Nesting these tags implies that you want to use grouped processing. However, only the top-level tag can specify the query that drives the processing.  
 
The error occurred in D:\inetpub\wwwroot\nextdaypro\wwwroot\test_site\includes\all_categories_links.cfm: line 62
 
60 : <form action="index.cfm" method="post" name="left_nav">
61 : <select name="left_nav_cats"  class="left_nav_select" multiple>
62 :   <cfoutput query="left_side_getCat">
63 : <option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
64 : </cfoutput>
 

<cfoutput>
 
<cfif structkeyexists(url,"category")>
<cfloop list="#url.category#" index="x">
    You have chosen Category #x#<br>
</cfloop>
 
 
</cfif>
 
<cfif structkeyexists(form,"left_nav_cats")>
<cfparam name="myURL" default="">
        <cfset formX = form.left_nav_cats>
        <cfloop list="#formX#" index="x">
          <cfset myUrl = MyUrl & #x# & ",">
    </cfloop>
 
 
<cfset myUrl= "index.cfm?category=#left(myUrl, len(myURL)-1)#">
<cflocation url="#myURL#">
</cfif>
 
<form action="index.cfm" method="post" name="left_nav">
<select name="left_nav_cats"  class="left_nav_select" multiple>
  <cfoutput query="left_side_getCat">
<option value="#left_side_getCat.category_ID#">#left_side_getCat.catName#</option>
</cfoutput>
 
</select>
<input type="submit" name="button" id="button" value="Submit">
</form></cfoutput>

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zac123

ASKER

ok, so this is now working and its worth pointing out that both sets of code work (Erics & Sid's).

unfortunately it is not possible to make this work with multiple selections because of the way the shopping cart i'm using it with handles the query without making some major alterations. so i've changed the select menu so that it does not allow multiple selections.

thanks to both of for your help yesterday.

zac