如何将 onclick 参数传递给 CFC 以通过 AJAX 更新数据库?
阿贾克斯新手。只是尝试做一个简单的 ajax/cfc 投票是/否应用程序。无法让它工作
我想要完成的是一个简单的投票“是或否”应用程序,它显示每个链接旁边的投票数。例如:
- 是(882 票)
- 否(163 票)。
当访问者投票时,数据库应该更新投票并将投票者记录在不同的表中(这样他们就不能再次投票)。最后,将显示一条确认消息,其中包含新的投票计数:
- 您投票了“是”(883 票) 或
- 您投票了否 (164 票)
现在除了更新数据库之外,一切都正常了。我尝试重新设计 JavaScript (AJAX),通过添加 ($.ajax)
并在 ajax 部分中移动响应消息来调用 CFC。然而,现在它根本不起作用。我做错了什么?
下面是我想出的新代码。为了简单起见,我只显示“否”票部分。我走在正确的轨道上吗?这看起来似乎非常简单。
投票链接
<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 *
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="MemberVote" DATASOURCE="MyDSN">
INSERT INTO MemberVoteRecord(IdeaID,MemberID,DatePosted,YesNo)
VALUES(#arguments.VoteNo#,#COOKIE.Member_ID#,#NOW()#,N)
</CFQUERY>
<cfreturn NewCount>
</cffunction>
New to Ajax. Just trying to do a simple ajax/cfc vote yes/no app. Can’t get it to work
What I am trying to accomplish is a simple Vote "Yes or No" app where it shows the number of votes cast next to each link. For example:
- Yes (882 votes)
- No (163 votes).
When a visitor votes, the database should be updated with the vote and record the voter in a different table (so they can't vote again). Finally, a confirmation message is displayed with the new vote count:
- You voted "Yes" (883 votes) or
- You voted No (164 votes)
Now I had everything working but updating the database. I tried reworking the JavaScript (AJAX) to call a CFC by adding ($.ajax)
and moving the response messages within the ajax part. However, now it’s not working at all. What did I do wrong?
Below is the new code I came up with. To keep this question simple, I am just showing the "No" Vote portion. Am I on the right track? This seem like it would be very 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 *
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="MemberVote" DATASOURCE="MyDSN">
INSERT INTO MemberVoteRecord(IdeaID,MemberID,DatePosted,YesNo)
VALUES(#arguments.VoteNo#,#COOKIE.Member_ID#,#NOW()#,N)
</CFQUERY>
<cfreturn NewCount>
</cffunction>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们的 .ajax() 调用的成功部分只是将 html 设置为变量#newcount#。我不确定该变量最初是在哪里设置的,但是如果您查看生成的原始 html,您会发现原始 html 此处包含一个静态数字。您需要将其设置为 javascript 变量,而不是 ColdFusion 变量。
因此,尝试将此变量设置为 message,这是传递给成功回调的参数。消息应等于 CFC 返回的任何内容。即,如果您直接在浏览器中调用 CFC,这就是您的浏览器将呈现的内容。
此外,您还需要将 CFC 函数的返回格式调整为“普通”。 。
The success part of our .ajax() call is just setting the html to be the variable #newcount#. I'm not sure where that variable is being set initially, but if you would look at your raw html generated, you will see that the raw html contains a static number here. You need to set this as a javascript variable, not a ColdFusion variable.
So, try setting this variable to be message, which is the argument passed to your success call back. Message should be equal to whatever your CFC is returning. I.e. if you would call the CFC directly in the browser, this is what your browser would render.
Also, you'll want to adjust your CFC function's return format to be "plain". .