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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query within javascript question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I'm using ColdFusion and a MS SQL7 database.

I have a number of text boxes on a form and what I need to do is fill in some of these fields automatically after the user types in info in two boxes. So what I'm doing is, the user fills in two boxes and with the "onchange" method, I'm calling a javascript function that first queries a table and then I need to populate some of the other fields on the form with the results from this query. Here is the function:

<SCRIPT LANGUAGE=&quot;javascript&quot;>
function find_info() {
if (document.myForm.CMD_DSG.value == &quot;&quot;) {
alert(&quot;You must fill in the CMD DSG field!&quot;);
return false;
} else {
<CFQUERY NAME=&quot;Get0000&quot; datasource=&quot;mynsnsql&quot;>
SELECT *, (fiacd_1+fiacd_2+fiacd_3+fiacd_45) as fiacd
FROM cmad0000
WHERE maj_key = ('C'+'C97R0030CZ')
</CFQUERY>
<CFOUTPUT QUERY=&quot;Get0000&quot;>
document.myForm.FIA_CD.value = &quot;#fiacd#&quot;;
document.myForm.NSN.value = &quot;#nsn#&quot;;
</CFOUTPUT>

return true;
}
}
</script>

When I hard code the info in the where clause, everything works fine, but my problem is, how can I get javascript to pass the values of these two fields into the where clause?
 
hi chester_

The problem here is that you're trying to access JS variables via a CF tag. You're also trying to activate CF tags via javascript.

The <CFQUERY> you're using is parsed by the server before HTML is sent to the browser. Since Javascript is activated by users and their web browsers after the HTML is received, what you're trying to do is like eating the tootsie center of a tootsie pop without cracking the candy shell.

To conditionally run CF tags, you have to use CF. To pass JS variables to a CF query, your CF template will have to access them via FORM. or URL.

There are more complex methods of retrieving queried data without refreshing the page. You can call a CFM template which returns an image with a specific height or width. Based on these image properties, you can tell what the result of the query was. This requires the onChange=&quot;&quot; javascript to call up an image who's src is actually a .CFM template. The CFM page uses

<cfcontent type=&quot;image/gif&quot; deletefile=&quot;NO&quot; file=&quot;/home/httproot/images/px.gif&quot;>

at the end to send back an image. Before this tag, you can perform updates and inserts, but retreiving data would be difficult because communicating the results back via an image would require a different image for every possible condition.

Answer1 is an image of height=1
Answer2 is an image of height=2 .. etc. not fun

hope you're clear now about why what you're doing is not possible


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top