• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

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

0
bede123
Asked:
bede123
  • 7
  • 5
  • 3
  • +1
4 Solutions
 
erikTsomikSystem Architect, CF programmer Commented:
can you try this
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?categoryID=#form. left_nav_cats#" 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>

or this

<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="" 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>

<cfif isDefined("form.button") and form.button eq "Submit">
  <cflocation url="results.cfm?categoryID=#form left_nav_cats.#" addtoken="no">
</cfif>

0
 
SidFishesCommented:
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

0
 
bede123Author Commented:
erikTsomik,

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

SidFishes,

i'm trying yours now

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bede123Author Commented:
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.

0
 
erikTsomikSystem Architect, CF programmer Commented:
what error you get
0
 
bede123Author Commented:

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

0
 
erikTsomikSystem Architect, CF programmer Commented:
try this. Sorry typo
<cfif isDefined("form.button") and form.button eq "Submit">
  <cflocation url="results.cfm?categoryID=#form.left_nav_cats#" addtoken="no">
</cfif>
0
 
SidFishesCommented:
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#">
0
 
bede123Author Commented:
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

0
 
SidFishesCommented:
you can try this..agian not sure what you're doing on the cflocation but this should give your the idea.



note: I added a comma <cfset myUrl = MyUrl & #x# & ","> so we can create a list

#left(myUrl, len(myURL)-1)# is to trim the trailing comma

then you lust loop over the list contain in url.category
<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" 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

0
 
bede123Author Commented:
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

0
 
duncancummingCommented:
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

0
 
SidFishesCommented:
just add back your original query loop

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


0
 
bede123Author Commented:
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

0
 
SidFishesCommented:
right... either change your cfoutput query =  to cfloop query =

or just take out the outer <cfoutput tags (top and bottom) and just add them where needed

cfif structkeyexists(url,"category")>
<cfoutput>
<cfloop list="#url.category#" index="x">
    You have chosen Category #x#<br>
</cfloop>
 </cfoutput>
</cfif>

0
 
bede123Author Commented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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