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

时间:2022-12-17 07:24:23

New to Ajax. Just trying to do a simple ajax/cfc vote yes/no app. Can’t get it to work

Ajax新手。只是尝试做一个简单的ajax / cfc投票是/否应用程序。无法让它发挥作用

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)
  • 是(882票)
  • No (163 votes).
  • 不(163票)。

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
  • 你投了“是”(883票)或者
  • You voted No (164 votes)
  • 你投了No(164票)

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?

现在我有一切工作但更新数据库。我尝试通过添加($ .ajax)并在ajax部分中移动响应消息来重新编写JavaScript(AJAX)来调用CFC。但是,现在它根本不起作用。我做错了什么?

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.

下面是我提出的新代码。为了简化这个问题,我只是展示了“No”投票部分。我是在正确的轨道上吗?这看起来很简单。

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

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>

1 个解决方案

#1


0  

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.

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

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.

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

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". .

此外,您还需要将CFC功能的返回格式调整为“普通”。 。

#1


0  

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.

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

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.

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

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". .

此外,您还需要将CFC功能的返回格式调整为“普通”。 。