如何将 onclick 参数传递给 CFC 以通过 AJAX 更新数据库?

发布于 2024-09-26 09:45:35 字数 2373 浏览 6 评论 0原文

阿贾克斯新手。只是尝试做一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-10-03 09:45:35

我们的 .ajax() 调用的成功部分只是将 html 设置为变量#newcount#。我不确定该变量最初是在哪里设置的,但是如果您查看生成的原始 html,您会发现原始 html 此处包含一个静态数字。您需要将其设置为 javascript 变量,而不是 ColdFusion 变量。

因此,尝试将此变量设置为 message,这是传递给成功回调的参数。消息应等于 CFC 返回的任何内容。即,如果您直接在浏览器中调用 CFC,这就是您的浏览器将呈现的内容。

success: function(message) {   
        $('#VoteNoMessage').html("you voted No");  
            $('#NewNoCount').html(**message**);  
           }   

此外,您还需要将 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.

success: function(message) {   
        $('#VoteNoMessage').html("you voted No");  
            $('#NewNoCount').html(**message**);  
           }   

Also, you'll want to adjust your CFC function's return format to be "plain". .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文