• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfquery within javascript

Guest
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

hi,

I am trying to do the following:

I wrote a cfquery within javascript function as below:

<script language="javascript1.2" type="text/javascript">
<cfquery name="query1 datasource="abc">
  ....
</cfquery>

<cfif query1.recordcount neq "0">
    <cfoutput query = "query1">
       <cfquery name="query2 datasource="abc">
  select * from table1 where name=#query1.name#
       </cfquery>
    </cfoutput>
     alert(<cfoutput>#query2.RecordCount#</cfoutput>);
</cfif>
     

</script>

The alert value is 1 but the debugging section shows 0, and 0 is the correct value. is there something wrong that I am doing.

If so can you please point out...

TOPICS
Advanced techniques

Views

5.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

The problem comes from how CF and JS work: Coldfusion is a "Server Side" language while Javascript is "Client Side". This means the server runs through all the CF code in your page and sends the finished product on to the user's browser which THEN starts handling the Javascript.

I'm not sure of your intended outcome ... do you want a series of "alerts" as soon as the page loads? If you are using the alert for debugging (just to see what is being returned, you can replace it with <p> tags and you will see the output on screen. Try this:

<cfif query1.recordcount neq "0">
     <cfoutput query = "query1">
          <cfquery name="query2 datasource="abc">
          select * from table1 where name=#query1.name#
          </cfquery>
          <p>#query2.RecordCount#</p>
     </cfoutput>
</cfif>

2 more notes:

  • you don't the inner "cfoutput" around you record output if you are going to use cfoutpu to loop over your query.
  • the output line needs to be INSIDE the loop or you'll only output the last iteration.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 14, 2010 Apr 14, 2010

Copy link to clipboard

Copied

As I have said many times:  ColdFusion runs on the server and JavaScript runs on the client and never will the two meet.  Since the server and the client could be thousands of miles apart.

Gabriel Landes wrote:

  • the output line needs to be INSIDE the loop or you'll only output the last iteration.

Well actuall since the recordCount does not change during each itteration of the loop, there is little need for it to be in the loop, it will always be the same value.  If one is outputing the queryVar.currentRot, putting it in the loop would be make sense, but not the queryVar.recordCount value.  If one is debugging the common issue of does this <cfif....> block work, I would suspect one would want to output the queryVar.recordCount value outside of the <cfif...> block.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 15, 2010 Apr 15, 2010

Copy link to clipboard

Copied

I don't know why you would want to go to all that trouble. However, you can convert a Coldfusion variable to the corresponding Javascript variable using the function toScript().  Your above code will then be something like

<cfquery name="query1" datasource="abc">
  ....
</cfquery>

<cfif query1.recordcount neq "0">
    <cfoutput query = "query1">
        <cfquery name="query2" datasource="abc">
              select * from table1 where name=#query1.name#
        </cfquery>
   
        <!--- Coldfusion recordcount variable --->
        <cfset cfRecordcount= query2.recordCount>
       
        <!--- Use toScript to convert from Coldfusion variable to Javascript variable --->
        <script language="javascript" type="text/javascript">
            var #toScript(cfRecordcount, "jsRecordcount")#;
            alert(jsRecordcount);       
        </script>
    </cfoutput>
</cfif>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2010 Apr 15, 2010

Copy link to clipboard

Copied

LATEST

You're looping over query1 running query2 for each record of query1, yet you're outputting query2.recordCount outside that loop, so you'll only be outputting the record count of the last record in query1.  Are you sure you're looking at the correct query in the debug output when you suggest there's a mismatch in the record counts on screen vs in the debug?  This seems terribly unlikely to me.  It's more likely you're confused, I think.

If you really do only want the record count of the last instance of query2... why are you looping over query1 at all?  Why not just grab query1.name[recordCount] directly?  Or better yet... revise your logic to only have one query if possible.  If one is running queries within a query loop, usually the logic is suboptimal.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation