Get the current value of a select box.

I need to find the current value of a select box and set it as a coldfusion variable.  Whats the best way to do this?

MFredinAsked:
Who is Participating?
 
azadisaryevConnect With a Mentor Commented:
ok, it seemd to be simpler than i thought... maybe i just got a bit lost in what exactly your were trying to do...
hope i got it right...

all you need to do is:
- make your cfc function accept one optional argument - the value of selected sale_buyer_id
- pass the currently selected sale_buyer_id value to your cfc function when you refresh the yards list.

2 js functions and cfc function need slight changes. see edited functions attached.

hope this works... haven't really tested it, but it should...

Azadi
<!--- this is your new getSaleBuyerYards js function: --->
 
var getSaleBuyerYards = function(sbid){ //uses ajax proxy to populate cfselect
 if (!sbid) sbid=0;
 var p = new salebuyeryardProxy();
 var populate = p.getSaleBuyerYards4cfselect(sbid);
 return populate;
};
 
 
<!--- and this is your new onHideAddSaleBuyerYard js function: --->
 
var onHideAddSaleBuyerYard = function(name){ //refreshes the cfselect
 var sbid = document.getElementById('sale_buyer_id').value;
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', getSaleBuyerYards(sbid));
};
 
 
<!--- and, finally, your new cfc function: --->
 
<cffunction name="getSaleBuyerYards4cfselect" access="remote" returntype="query" output="false">
<cfargument name="sbid" required="no" default="0">
<cfset var qGetSaleBuyerYards="">
<cfquery name="qGetSaleBuyerYards" datasource="#request.dsn#">
SELECT sale_buyer_yard_id, sale_buyer_yard
FROM sale_buyer_yard
<cfif arguments.sbid gt 0>
WHERE sale_buyer_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.sbid#">
</cfif>
ORDER BY sale_buyer_yard
</cfquery>
<cfreturn qGetSaleBuyerYards />
</cffunction>

Open in new window

0
 
duncancummingCommented:
Form field looks like
<select name="SelectBoxName">
  <option value="Blah">Option 1</option>
  <option value="Foo">Option 2</option>
</select>

Submit the form to a Coldfusion page.  The value of the select box will be available as
Form.SelectBoxName


0
 
MFredinAuthor Commented:
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
SidFishesConnect With a Mentor Commented:
as per other q

remember that js values are client side only  To pass a js variable to a cfc you need use a form variable or use cfinvoke on your calling page.

ie if the url variable being passed in your cfwindow call was

add_SaleBuyerYard.cfm?sale_buyer_id=123

you'd

<cfinvoke ...>
         <cfinvokeargument name="sale_buyer_id" value="#ur;sale_buyer_id#">
</cfinvoke>

but since you are posting a form from the calling page, i think you'd probably just use the value from sale_buyer_id on your main form




0
 
SidFishesCommented:
value="#ur;sale_buyer_id#

sb

value="#url.sale_buyer_id#
0
 
MFredinAuthor Commented:
Hmmm.. ok.  I will explain what I am trying to do.


When I add a new Sale_Buyer_Yard using the CFWINDOW in the last question, it fires the javascript:getSaleBuyerYards in my cfc to refill the sale_buyer_yard_id cfselect box. What I am missing from it is the WHERE clause to tell it which sale_buyer_id.  Right now its just returning all sale_buyer_yards since I have no WHERE clause.  I need it to return the results WHERE sale_buyer_id = document.getElementById('sale_buyer_id').value.  Just not sure how to do that.   So I guess I need to figure out how to pass document.getElementById('sale_buyer_id').value to the cfc and use it in the WHERE clause.
<!--- Sale Buyer --->
<cfselect name="sale_buyer_id" id="sale_buyer_id" size="1" bind="javascript:getSaleBuyers()" bindonload="true" display="sale_buyer_Name" value="sale_buyer_id" selected="#sale.sale_buyer_id#" />
 
<!--- Sale Buyer Yard --->
<cfselect name="sale_buyer_yard_id" id="sale_buyer_yard_id" bind="javascript:getSaleBuyerYards()" bindonload="true" display="sale_buyer_yard" value="sale_buyer_yard_id" />

Open in new window

0
 
MFredinAuthor Commented:
SidFishers,

Does that make sense?  I upped the points on this question... it's been stumping me for days!
0
 
azadisaryevCommented:
what does your getSaleBuyerYards js function look like?
if it uses a cfc proxy, post the cfc function call as well.

Azadi
0
 
MFredinAuthor Commented:
Hi Azadi,

Do you mean my cfc?  

FYI: This question is somewhat of a continuation of this question... http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_24278303.html



<!--- Sale Buyer Yards CFC to fill the cfselect query --->
<cffunction name="getSaleBuyerYards4cfselect" access="remote" returntype="query" output="false">
<cfset var qGetSaleBuyerYards="">
 
<cfquery name="qGetSaleBuyerYards" datasource="#request.dsn#">
SELECT sale_buyer_yard_id, sale_buyer_yard
FROM sale_buyer_yard
WHERE sale_buyer_id = THE document.getElementById('sale_buyer_id').value FROM THE CALLING FORM
ORDER BY sale_buyer_yard
</cfquery>
 
<cfreturn qGetSaleBuyerYards />
</cffunction>

Open in new window

0
 
azadisaryevCommented:
i also mean you js function the cfselect is bound to: bind="javascript:getSaleBuyerYards()".
getSaleBuyerYards() javascript function - can you post its code, please?

i also assume you are using the function to open cfwindow Sid posted in your other question, right?

Azadi
0
 
MFredinAuthor Commented:
Sorry... it actually should be bind="cfc:addWindow.getSaleBuyerYards4cfselect()".

To open the cfwindow I am calling the addSaleBuyerYard() function via <a href="javascript:addSaleBuyerYard();">[ add new ]</a>.






<cfajaxproxy cfc="addWindow" jsclassname="salebuyeryardProxy">
 
<!--- js functions --->
<script type="text/javascript">
var openWindow = function(name,title,url,options){
 ColdFusion.Window.create(name, title, '', options);
 ColdFusion.navigate(url, name);
};
 
<!--- Functions specific to adding a new SALE BUYER YARD --->
var addSaleBuyerYard = function(){ //opens add_Salebuyeryard.cfm page in a cfwindow for adding new yard data
var param = document.getElementById('sale_buyer_id').value;
ColdFusion.Window.create('sale_buyer_yard_id_window', 'Add Sub-Yard', 'add_SaleBuyerYard.cfm?sale_buyer_id=' + param, {center:true,height:300,width:380,closable:true,dragable:true,resizable:true,modal:true,initshow:true});
};
 
var getSaleBuyerYards = function(){ //uses ajax proxy to populate cfselect
 var p = new salebuyeryardProxy();
 var populate = p.getSaleBuyerYards4cfselect();
 return populate;
};
 
var onHideAddSaleBuyerYard = function(name){ //refreshes the cfselect
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', getSaleBuyerYards());
};

Open in new window

0
 
MFredinAuthor Commented:
So what I really need to do is pass the document.getElementById('sale_buyer_id').value into the getSaleBuyerYards4cfselect() cfc query.
0
 
azadisaryevCommented:
ok.
can you also post the code of the page you display in your cfwindow, so i can make a complete code sample for you?

Azadi
0
 
azadisaryevCommented:
actually, you can totally get rid of getSaleBuyerYards js function and just change your onHideAddSaleBuyerYard function to this:

var onHideAddSaleBuyerYard = function(name){ //refreshes the cfselect
 var p = new salebuyeryardProxy();
 var sbid = ColdFusion.getElementValue('sale_buyer_id');
 ColdFusion.Bind.assignValue('sale_buyer_yard_id', 'value', p.getSaleBuyerYards4cfselect(sbid));
};

Azadi
0
 
MFredinAuthor Commented:
It worked!!!!  Perfect!

The only other thing I am missing now is when the page loads, all sale_buyer_yard_ids get listed since sbid has no value.  Is there a way to pass the sbid argument to the getSaleBuyerYards4cfselect onload?  So it selects the correct values for the initial record in sale_buyer_id?

I'm also getting a blank CFWINDOW page after I insert a new record... I think I have to clear the other one somehow?  Not a big deal but if its easy, it would be nice to add.  

0
 
azadisaryevCommented:
> So it selects the correct values for the initial record in sale_buyer_id?

what's the correct value and where is it stored (which cf or js variable)?

> I'm also getting a blank CFWINDOW page after I insert a new record

not quiet sure what you mean - is this after you click the 'submit' button in the page displayed in cfwindow, or is it when you try to add another sale_buyer_yard ?

Azadi
0
 
MFredinAuthor Commented:
When the page loads, the sale_buyer_id select loads the full list of sale_buyers (getSaleBuyers4cfselect function).  

Then the sale_buyer_yard_id select loads the getSaleBuyerYards4cfselect function but returns all records in the sale_buyer_yard table.  I need it to load the records for the selected value in sale_buyer_id.

The query in the getSaleBuyerYards4cfselect function looks like this..  onload, it does not include the WHERE clause since we haven't passed any value for sbid.  The sbid value should be the value of the initially loaded sale_buyer_id.  That way it will select only it's records when the page loads.

<cfquery name="qGetSaleBuyerYards" datasource="#request.dsn#">
SELECT sale_buyer_yard_id, sale_buyer_yard
FROM sale_buyer_yard
<cfif arguments.sbid gt 0>
WHERE sale_buyer_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.sbid#">
</cfif>
ORDER BY sale_buyer_yard
</cfquery>




When I add a new sale_buyer_yard_id using CFWINDOW, the first one works great.  When I click to add another, the CFWINDOW pops up, but its blank.

0
 
SidFishesCommented:
" When I click to add another, the CFWINDOW pops up, but its blank."

probably a caching problem

you can try adding a unique id (wid) based on time to your window and also clearing the object cache after creating the window


var addSaleBuyerYard = function(){

var d = new Date()
var wid = d.getMinutes()+d.getSeconds()
var param = document.getElementById('sale_buyer_id').value;
 ColdFusion.Window.create('sale_buyer_yard_id_window'+wid, 'Add Sub-Yard', 'add_SaleBuyerYard.cfm?sale_buyer_id' + param, {center:true,height:300,width:380,closable:true,dragable:true,resizable:true,modal:true,initshow:true});

ColdFusion.objectCache["sale_buyer_yard_id_window"+wid = null;
};








0
 
azadisaryevCommented:
> I need it to load the records for the selected value in sale_buyer_id.

which is the selected record in sale_buyer_id when the page loads? which variable is it stored in?
what you can try is change your sale_buyer_yard_id select to:

<cfselect name="sale_buyer_yard_id" id="sale_buyer_yard_id" bind="cfc:addWindow.getSaleBuyerYards4cfselect({sale_buyer_id})" bindonload="false" display="sale_buyer_yard" value="sale_buyer_yard_id" />

the above will load sale_buyer_yard_id with the values related to selected sale_buyer_id only (or related to the first sale_buyer_id if none was pre-selected on initial page load)

> When I click to add another, the CFWINDOW pops up, but its blank

you could use ColdFusion.navigate() fucntion instead of ColdFusion.Window.create() in your addSaleBuyerYard js function. you will need to already have a hidden empty cfwindow in your page for this to work:

a) add <cfwindow name="sale_buyer_yard_id_window" title="Add Sub-Yard" center="true" height="300" width="380" closable="true" dragable="true" resizable="true" modal="true" initshow="false"></cfwindow>
to the bottom of your page, just before </body> tag

and change your addSaleBuyerYard js function to:

var addSaleBuyerYard = function(){
var param = document.getElementById('sale_buyer_id').value;
ColdFusion.navigate( 'add_SaleBuyerYard.cfm?sale_buyer_id=' + param, 'sale_buyer_yard_id_window');
ColdFusion.Window.show('sale_buyer_yard_id_window');
};

Azadi
0
 
MFredinAuthor Commented:
Perfect!! The only thing I have left is to fire the getSaleBuyerYards4cfselect function after I have entered a new sale_buyer_id which would requery to get the sale_buyer_yard_ids for that newly added sale_buyer_id.  Otherwise, the values in sale_buyer_yard_id are incorrect.

I assume I could add something to the onHideAddSaleBuyer fucntion?
0
 
azadisaryevCommented:
was that ever part of your original question? this has been going on for so long i forget what was the original probelm... :)
i definitely do not see any code for onHideAddSaleBuyer function posted anywhere before... so why don;t you start a new question? :)

Azadi
0
 
MFredinAuthor Commented:
Thanks for the help Azadi!

I opened a new question about refiring the getSaleBuyerYards4cfselect function after a new sale_buyer_id is entered here...

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_24284602.html

Please take a look.

Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.