coldfusion - How do I pass an onclick argument to a CFC to update the database through AJAX? -
new ajax. trying simple ajax/cfc vote yes/no app. can’t work
what trying accomplish simple vote "yes or no" app shows number of votes cast next each link. example:
- yes (882 votes)
- no (163 votes).
when visitor votes, database should updated vote , record voter in different table (so can't vote again). finally, confirmation message displayed new vote count:
- you voted "yes" (883 votes) or
- you voted no (164 votes)
now had working updating database. tried reworking javascript (ajax) call cfc adding ($.ajax)
, moving response messages within ajax part. however, it’s not working @ all. did wrong?
below new code came with. keep question simple, showing "no" vote portion. on right track? seem simple.
voting link
<a href="javascript:()" onclick="votenoid('#ideaid#');"> <span id="votenomessage">no</span> </a> - <span id="newnocount">#nocount#</span>
ajax
<script language="javascript"> function votenoid() { var votenodescription = document.getelementbyid("votenodescription").style.display='none'; $.ajax({ type: "post", url: "../cfc/voteno.cfc?method=votenoid", data: recordata, datatype: "html", success: function(message) { $('#votenomessage').html("you voted no"); $('#newnocount').html("#newcount#"); } }); }); } ` <script>
voteno.cfc
<cffunction name="novote" access="remote" returntype="string" <cfargument name="voteno" type="string" required="yes"> <cfquery name="nocountck" datasource="mydsn"> select * ideas ideaid = #arguments.voteno# </cfquery> <cfset newcount=#nocountck.nocount#+1> <cfquery name="updatenocount" datasource="mydsn"> update ideas set nocount = #newcount# ideaid = #arguments.voteno# </cfquery> <cfquery name="membervote" datasource="mydsn"> insert membervoterecord(ideaid,memberid,dateposted,yesno) values(#arguments.voteno#,#cookie.member_id#,#now()#,n) </cfquery> <cfreturn newcount> </cffunction>
the success part of our .ajax() call setting html variable #newcount#. i'm not sure variable being set initially, if @ raw html generated, see raw html contains static number here. need set javascript variable, not coldfusion variable.
so, try setting variable message, argument passed success call back. message should equal whatever cfc returning. i.e. if call cfc directly in browser, browser render.
success: function(message) { $('#votenomessage').html("you voted no"); $('#newnocount').html(**message**); }
also, you'll want adjust cfc function's return format "plain". .
Comments
Post a Comment