0

Now I have everything working but my cfc is not updating the database. I tried changing the code in the cfc page, but that just brings up errors when I refresh the page to test it. So I do know it is calling up the cfc page. But when I click the link, everything works except updating the database. What did I do wrong?

Below is the code I came up with. It's just the No Vote part of a Yes/No Vote App. to keep this question simple. Am I on the right track? This seems like it should be very simple.

Voting link

<A HREF="javascript:()" onclick="VoteNoID('#IdeaID#');"><SPAN ID="VoteNoMessage">I Vote No</SPAN></A> - <SPAN ID="NewNoCount">#NoCount#</SPAN>

Ajax / cfajaxproxy

<cfajaxproxy cfc="CFC/MyCFC" jsclassname="MyCFC">  
<script LANGUAGE="JavaScript">  
 function VoteNoID()    
           {    
            var VoteNoID = document.getElementById("VoteNoID");   
            var cfc = new MyCFC();     
             cfc.setCallbackHandler(getDataResult);   
             cfc.NewCount(true)  
            var VoteNoDescription = document.getElementById("VoteNoDescription").style.display='none';  
    $('#NewNoCount').html("");
    $('#VoteNoMessage').html('You voted "No" with');
           }  
       function getDataResult(result)  
           {  
            document.write(result);  
           }  
</script>

VoteNo.cfc

<cfcomponent>
  <cffunction name="NewCount" access="remote">
    <cfargument name="VoteNo" required="yes">
    <CFQUERY NAME="NoCountCK" DATASOURCE="MyDSN">
      SELECT *
      FROM Ideas
      WHERE IdeaID = #arguments.VoteNo#
    </CFQUERY>
    <CFSET NewCount=#NoCountCK.NoCount#+1>
    <CFQUERY NAME="UpdateNoCount" DATASOURCE="MyDSN">
      UPDATE  Ideas
      SET  NoCount = #NewCount#
      WHERE IdeaID = #arguments.VoteNo#
    </CFQUERY>
    <CFQUERY NAME="Member" DATASOURCE="MyDSN">
      SELECT *
      FROM Members
      WHERE MemberID = 1
    </CFQUERY>
    <CFQUERY NAME="MemberVote" DATASOURCE="MyDSN">
      INSERT INTO ComingSoonVote(CSID,MemberID,DatePosted,YesNo)
      VALUES(#arguments.VoteNo#,#COOKIE.MemberID#,#NOW()#,N)
    </CFQUERY>
    <cfreturn NewCount>
  </cffunction>
</cfcomponent>    

Edit Ok to bring everyone up to date. I removed the Query "MemberVote" (yes it's still here in the code above. I left it here for everyone to see it) just to get around the Error and see how the NewCount is working after adding cfc.NewCount(true) like orangepips suggested. What I get is this; It does update the database and shows the NewCount just as it is suppose to do. But then 1 second later, the page goes white and all I have is the NewCount displayed in the top left (nothing else).

Firebug shows nothing and what I mean is Nothing (blank). No HTML, CSS, no code at all. Somehow the ajax refreshed and is only displaying the NewCount and the page is acting like it's trying to lode something. Did I forget to add something to keep it from refreshing or is it something else?

I need to fix this new problem and get it to add the members vote to the database in the in the "MemberVote" query. Any ideas?

3 Answers 3

2

Without seeing an actual error, my guess is that your problem is with using an unscoped variable name the same as a function. Instead of and putting that just after the argument tag. Then later in your code use the countNew variable.

One other thing...please, plesae, plesae use cfqueryparam in your queries when you are passing in variables. This protects you from sql injection attacks (and improves performance). For instance:

<CFQUERY NAME="NoCountCK" DATASOURCE="MyDSN">
SELECT *
FROM Ideas
WHERE IdeaID = <cfqueryparam value="#arguments.VoteNo#">
</CFQUERY>
1
  • The error was something I made the cfc do just to see if it was even being called up. Not to worry Sam I always use cfqueryparam on all my queries. Just didn’t put them in my example as I wanted to keep the code simple so someone could see what I did wrong. I put the "NewCount" variable in and that solved updating the count in the database but that is as far as it works. The query insert is still not working. I am going to try some different things and see if I might get something right. Oct 9, 2010 at 18:32
0

First, I think you're never calling the "NewCount" function in the MyCFC component. As the cfajaxproxy docs demonstrate (http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_a-b_3.html), you call cfajaxproxy, initialize a JavaScript object, and call a method on that object. So in your case it would be "cfc.NewCount(true)" (note this is case sensitive) after the "cfc.setCallbackHandler(getDataResult);".

Next, to check yourself, use Firefox with Firebug. This will allow you to easily JavaScript errors and AJAX requests. Load your page with Firebug running. Check the Firebug console for JavaScript errors. If it's complaining it can't find the library cfajax.js then you probably need to setup a web server mapping to the CFIDE directory - where ColdFusion keeps its various JavaScript files that it ships with.

Finally, click the "Vote" button. Check the AJAX requests in Firebug to see something's fired off. See what the response code is. Not 200? Open the request in the Firebug console to see the response HTML. You can right click the response to open in a new browser tab.

5
  • Thanks orangepips I did put the "NewCount" variable in after the "cfc.setCallbackHandler(getDataResult);" and that did solved updating the count in the database but that is as far as it works. The query insert is still not working. I am now getting an “Error Executing Database Query” error. I am going play around with it, I might get lucky. If you see something please let me know. Oct 9, 2010 at 18:32
  • I updated the code above and add what is going on with it now. Oct 9, 2010 at 20:40
  • Looks to me like the MemberVote cfquery call is missing single quotes around the "N" as the last value to be inserted. Also, are you getting anything in exception.log?
    – orangepips
    Oct 10, 2010 at 23:50
  • Yes they are but that wasnt the problem. when I chaned the YesNo feild name all was good. I also changed document.write() to var content = document.getElementById('NoCount').innerHTML=result; which stopped it from overwritting my dom Oct 12, 2010 at 2:18
  • Can you go look at http://stackoverflow.com/questions/3910945/how-to-pass-an-onclick-link-value-to-javascript-and-cfc-though-cfajaxproxy, see if you know what to do? Oct 12, 2010 at 2:20
0

Ok I fixed the document.write overwriting the entire DOM by replacing:

    document.write(result) 

with this

    var content = document.getElementById('NewCount').innerHTML=result; 

This seems to keep the change within the <SPAN ID=" NewCount'> </SPAN> and not overwriting my page.

I fixed why my cfc wouldn't add the members vote to the database in the "MemberVote" query. I was getting an "Error invoking CFC: Error Executing Database Query". I had to re-label a field in the table. For some reason executing the table though the cfc didn’t work when it works outside the cfc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.