Zac123
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.
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
erikTsomik,
your first option has no effect. the second option throws an error.
SidFishes,
i'm trying yours now
your first option has no effect. the second option throws an error.
SidFishes,
i'm trying yours now
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.
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
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\nextday
33 :
34 : <cfif isDefined("form.button") and form.button eq "Submit">
35 : <cflocation url="results.cfm?categoryI
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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#">
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
<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#">
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:
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok so have inserted the code as above and replaced the <option value="1">1</option>s with <option value="#left_side_getCat.c ategory_ID #">#left_s ide_getCat .catName#< /option>
but how exactly do i get all the list/option items to disply? because currently there is only one displaying.
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>
Change your form from POST to GET. This passes all form variables in the URL
<form action="results.cfm" method="GET" name="left_nav">
just add back your original query loop
<cfoutput query="left_side_getCat">
<option ...
<cfoutput query="left_side_getCat">
<option ...
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\nextday pro\wwwroo t\test_sit e\includes \all_categ ories_link s.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.c ategory_ID #">#left_s ide_getCat .catName#< /option>
64 : </cfoutput>
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\nextday
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.c
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Open in new window