View Full Version : Using onchange to update a variable when select option is chosen..
deserel
09-17-2004, 04:54 PM
Hi,
Need to update a variable that I am using in a query when someone selects a new option from a drop down menu.
Example...
<select name="ProductType" onchange="LocalProductTypeID=this.form.ProductType">
<option value="">Choose One</option>
<option value="1">Product Type 1</option>
<option value="2">Product Type 2</option>
</select>
That is then used in a query
SELECT *
FROM dbo.kbproducts
where ProductTypeID = #LocalProductTypeID#
order by ProductName
I suppose I could call a function that then assigns this value to a variable, but is there a more direct way?
Currently, I have a button
<input type ="Submit" Name = "SelectType" Value="Select">
Which the user has to press to update the page, I want this to happen as soon as the value is selected instead of pressing the button.
Thanks!
Brett
Willy Duitt
09-17-2004, 05:24 PM
What about: onchange="this.form.submit()".....
deserel
09-17-2004, 05:44 PM
Perfect! Thanks.
deserel
09-17-2004, 06:19 PM
Along the same lines, when a new item is selected, I also need to clear a variable.
So, the user is selecting the product type. A list of products is then displayed for the user to pick from.
When the user picks a product, the description is displayed.
When a user picks a product then picks another product type, the product description is still being displayed.
So, when productTypeID is changed, I need to clear the value of productID
I've tried both
<select name="LocalProductTypeID" onchange="this.form.submit();form.clear('LocalProductID')">
and
<select name="LocalProductTypeID" onchange="this.form.submit();LocalProductID='')">
But neither work.
Willy Duitt
09-17-2004, 06:27 PM
Remove the errant ) in your second example...
Oh... and place that before the submit or the form may submit prior to the variable being updated....
deserel
09-17-2004, 08:32 PM
Hmmmm.. I have
<select name="LocalProductTypeID" onchange="LocalProductID=''; this.form.submit()">
And I am getting the JavaScript error ...
"Object doesn't support this property or method."
hemebond
09-17-2004, 10:53 PM
document.getElementById("LocalProductID").value = '';Gecko DOM Reference (http://www.mozilla.org/docs/dom/domref/)
Willy Duitt
09-17-2004, 11:01 PM
I thought the O/P previously asserted that LocalProductID was a variable.... and I would have assumed that it was a global variable...
But the error which seems to be returned makes me believe that it is a local not global variable....
Hard to tell with so little to go on...
.....Willy
hemebond
09-17-2004, 11:38 PM
I thought the O/P previously asserted that LocalProductID was a variable.... and I would have assumed that it was a global variable...Well if it is, there's lot much point in reseting it unless he's also overridden the onsubmit event for his form.
deserel
09-22-2004, 03:56 PM
Yes, it is a global variable...
At the very top of the page...
<CFPARAM NAME="LocalProductID" DEFAULT="">
Then my condition
<CFIF #LocalProductID# is not "">
...snip
</cfif>
(Thus, if the product type is selected to a new value, I need to reset LocalProductID)
finally...
<select name="LocalProductTypeID" onchange="LocalProductID=''; this.form.submit();">
<option value="">Choose One</option>
<option value="ALL">View All</option>
<option value="1">Software</option>
<option value="2">Website</option>
</select>
deserel
09-22-2004, 04:04 PM
Just a little more info...
Here are two lines. 104 is the one I am playing with
104: <select name="LocalProductTypeID" onchange="LocalProductID=''; this.form.submit();">
105: <option value="">Choose One</option>
According to the Javascript error, it is on line 105, char 1. "Object does not support this property or method." That suggests (to me) that something isn't getting closed right on 104.
LocalProductID ='' is two single quotes..
If I remove LocalProductID=''; then the Javascript error goes away.
Thanks again!
Willy Duitt
09-22-2004, 04:13 PM
I've never worked with cold fusion but it is my assumption that your cf variable: <CFPARAM NAME="LocalProductID" DEFAULT=""> is not global nor recognized by the javascript which is why LocalProductID ='' is throwing an error (most likely such and such is null or not an object or some such)
I'm not sure if you can use the cf variabble as a value for the javascript variable:
LocalProductID ='<CFPARAM NAME="LocalProductID" DEFAULT="">';
But somewhere in there is where your problem lies...
.....Willy
deserel
09-22-2004, 07:43 PM
OK, I am going a different route.
I am putting up a button to "Clear the form"
<input type="button" name="Clear" value ="Reset the form" onclick="document.myform.reset(); document.myform.submit();" />
The form is
<form name="myform" action="index.cfm" method="post">
For testing, I put in <input type="button" name="Clear" value ="clear" onclick="alert('You clicked a button');" /> and it works (I get the alert).
But, otherwise, this isn't clearing the form variables. It seems to be resubmitted the form with the same variables. (i.e. it does not clear out what the user has selected).
Willy Duitt
09-22-2004, 07:52 PM
<input type="reset" value="Reset"> :)
deserel
09-22-2004, 07:59 PM
<input type="reset" value="Reset"> :)
That doesn't do anything... this is driving me crazy! :) This is such a simple thing.
There doesn't need to be any sort of action associated with the button?
Willy Duitt
09-22-2004, 08:04 PM
http://www.w3.org/TR/REC-html40/interact/forms.html#form-controls
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.