Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

connecting two SQL query with Javascript

Status
Not open for further replies.

fic

Programmer
Apr 10, 2000
2
ES
I`ve got two tables in a database called countrys and cities.<br>There is a select tag for each table with their SQL querys.<br>If I select United States in the country select I want the cities select to be refreshed so i can only see the cities that belong to United States.<br><br>Is there anyway to do this dynamically?<br><br>Thanx.
 
You could do this with frames...&nbsp;&nbsp;<br><br><b>FRAME ONE</b><br><FONT FACE=monospace>&lt;cfquery name=&quot;Countries&quot; datasource=&quot;mydb&quot;&gt;<br>&nbsp;&nbsp;select CountryID,Country from Country_Table<br>&nbsp;&nbsp;order by Country<br>&lt;/cfquery&gt;<br><br>&lt;form action=&quot;frame2.cfm&quot; method=&quot;post&quot; target=&quot;frame2&quot;&gt;<br>&nbsp;&nbsp;&lt;select name=&quot;CountryID&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfoutput query=&quot;Countries&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;#CountryID#&quot;&gt;#Country#&lt;/option&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/cfoutput&gt;<br>&nbsp;&nbsp;&lt;/select&gt;<br>&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;GO&quot;&gt;<br>&lt;/form&gt;</font><br><br><br><b>FRAME TWO</b><br><FONT FACE=monospace>&lt;cfparam name=&quot;CountryID&quot; default=0&gt;<br>&lt;cfif stateid&gt;<br>&nbsp;&nbsp;&lt;cfquery name=&quot;cities&quot; datasource=&quot;mydb&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;select CityID,City from Cities<br>&nbsp;&nbsp;&nbsp;&nbsp;where CountryID=#CountryID#<br>&nbsp;&nbsp;&nbsp;&nbsp;order by City<br>&nbsp;&nbsp;&lt;/cfquery&gt;<br><br>&nbsp;&nbsp;&lt;form action=&quot;frame3.cfm&quot; method=&quot;post&quot; target=&quot;frame3&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;select name=&quot;CityID&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfoutput query=&quot;cities&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;#CityID#&quot;&gt;#City#&lt;/option&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/cfoutput&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;GO&quot;&gt;<br>&nbsp;&nbsp;&lt;/form&gt;<br>&lt;/cfif&gt;</font><br><br><br><b>FRAME THREE</b><br><FONT FACE=monospace>&lt;cfparam name=&quot;CityID&quot; default=0&gt;<br>&lt;cfif CityID&gt;<br>&nbsp;&nbsp;&lt;cfquery name=&quot;city&quot; datasource=&quot;mydb&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select * from cities<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where CityID=#CityID#<br>&nbsp;&nbsp;&lt;/cfquery&gt;<br>&nbsp;&nbsp;&lt;cfoutput query=&quot;city&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- Put city information code here --&gt;<br>&nbsp;&nbsp;&lt;/cfoutput&gt;<br>&lt;/cfif&gt;</font><br><br>Let me know if this works for you.&nbsp;&nbsp;(btw, you could put javascript code in the select boxes to submit the form upon selection, it should still work...)
 
I'm fairly new to CF so it took me quite some time to work this one out. This particular section uses Combo boxes (I don't know if that's how you wanted to do it).It works by using the CFWDDX cfml2js Action tag to output the query results into a javascript array. Javascript can then be used to dynamically update the combo's. Bit long, but it works.<br><br><br><br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;title&gt;Untitled Document&lt;/title&gt;<br>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;&gt;<br><br>&lt;script language=&quot;JavaScript&quot;&gt;<br><br>&lt;CFINCLUDE template='/CFIDE/scripts/wddx.js'&gt;<br><br>// used in refereshTable() to remember the length of the previous array<br>var oldLength = 0;<br><br>function refreshTable() {<br><br> var selectedArray = eval(document.genquery.chooseTable.value + &quot;Array&quot;);<br><br> // empty the contents of the options list<br> for (var i=0; i &lt; oldLength; i++) {<br> document.genquery.chooseField.options<i> = null;<br> }<br><br> // loop through the array, dynamically creating the 2nd combo options<br> for (var i=0; i &lt; selectedArray.length; i++) {<br> /* the \u codes in this line are unicodes for special characters, in this case ( ' ) */<br> eval(&quot;document.genquery.chooseField.options<i> = new Option \u0028\u0027&quot; + selectedArray<i>[0] + &quot;\u0027,\u0027&quot; + selectedArray<i>[1] + &quot;\u0027\u0029&quot;);<br> }<br> oldLength = selectedArray.length;<br><br>}<br><br>&lt;/script&gt;<br><br>&lt;/head&gt;<br><br>&lt;body bgcolor=&quot;#FFFFFF&quot;&gt;<br><br>&lt;cfquery name=&quot;getTables&quot; datasource=&quot;Intranet&quot; dbtype=&quot;ODBC&quot;&gt;<br> select * from TableList where tName = 'All' order by fValue<br>&lt;/cfquery&gt;<br><br>&lt;!--- Queries from which JS arrays are generated ---&gt;<br>&lt;cfquery name=&quot;getOrders&quot; datasource=&quot;Intranet&quot; dbtype=&quot;ODBC&quot;&gt;<br> select tFields,fValue from TableList where tName = 'Orders' order by fValue<br>&lt;/cfquery&gt;<br>&lt;cfquery name=&quot;getQuotes&quot; datasource=&quot;Intranet&quot; dbtype=&quot;ODBC&quot;&gt;<br> select tFields,fValue from TableList where tName = 'Quotes' order by fValue<br>&lt;/cfquery&gt;<br><br>&lt;script language=&quot;javascript&quot;&gt;<br><br>/////////////////////////////////////////////<br>/* generate an array for each query result */<br>/////////////////////////////////////////////<br><br>&lt;CFWDDX ACTION='cfml2js' input=#getOrders# topLevelVariable='q'&gt;<br><br>// get the number of rows from the query (getRowCount() comes from the above template file)<br>var nRows = q.getRowCount();<br>// Init Array<br>var oentArray = new Array();<br>// Init array's 2nd dimension<br>for (i=0; i &lt; nRows; i++) {<br> oentArray<i> = new Array(2); }<br>// read descriptions and values into array<br>for (i=0; i&lt;nRows; i++) {<br> oentArray<i>[0] = q.getField(i, 'tfields');<br> oentArray<i>[1] = q.getField(i, 'fvalue');<br>}<br><br><br>&lt;CFWDDX ACTION='cfml2js' input=#getQuotes# topLevelVariable='q'&gt;<br><br>// get the number of rows from the query (getRowCount() comes from the above template file)<br>var nRows = q.getRowCount();<br>// Init Array<br>var qenqArray = new Array();<br>// Init array's 2nd dimension<br>for (i=0; i &lt; nRows; i++) {<br> qenqArray<i> = new Array(2); }<br>// read descriptions and values into array<br>for (i=0; i&lt;nRows; i++) {<br> qenqArray<i>[0] = q.getField(i, 'tfields');<br> qenqArray<i>[1] = q.getField(i, 'fvalue');<br>}<br><br><br>&lt;/script&gt;<br><br><br>&lt;form method=&quot;post&quot; action=&quot;&quot; name=&quot;genquery&quot;&gt;<br><br>&nbsp;&nbsp;&lt;select name=&quot;chooseTable&quot; onChange=&quot;refreshTable()&quot;&gt;<br>&nbsp;&nbsp; &lt;option selected value=&quot;&quot;&gt;&lt;/option&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;cfoutput query=&quot;getTables&quot;&gt;<br> &lt;option value=&quot;#fValue#&quot;&gt;#tFields#&lt;/option&gt;<br> &lt;/cfoutput&gt;<br>&nbsp;&nbsp;&lt;/select&gt;<br><br>&nbsp;&nbsp;&lt;select name=&quot;chooseField&quot;&gt;<br>&nbsp;&nbsp;&lt;/select&gt;<br>&nbsp;&nbsp;&lt;input type=&quot;button&quot; value=&quot;Show Value...&quot; onClick=&quot;alert(document.genquery.chooseField.value)&quot;&gt;<br><br>&lt;/form&gt;<br><br>&lt;/body&gt;<br>&lt;/html&gt;<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top