加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将ajax数据发布到PHP并返回数据

发布时间:2020-12-16 03:12:40 所属栏目:百科 来源:网络整理
导读:如何将一些ajax数据发布到控制器功能并将其恢复?因为我想在函数中发布一个整数,并获得另一个整数(对于发布ID的项目的总投票数),并且在成功时我希望回显该投票计数. 我不知道如何将“id”发布到控制器功能.请看我的代码: //post this integetthe_id = $(thi
如何将一些ajax数据发布到控制器功能并将其恢复?因为我想在函数中发布一个整数,并获得另一个整数(对于发布ID的项目的总投票数),并且在成功时我希望回显该投票计数.
我不知道如何将“id”发布到控制器功能.请看我的代码:
//post this integet
the_id = $(this).attr('id'); 

        $.ajax({
            type: "POST",data: the_id,url: "http://localhost/test/index.php/data/count_votes",success: function(){
                //the controller function count_votes returns an integer.
                //echo that with the fade in here.

                }
            });
$.ajax({
            type: "POST",data: {data:the_id},success: function(data){
               //data will contain the vote count echoed by the controller i.e.  
                 "yourVoteCount"
              //then append the result where ever you want like
              $("span#votes_number").html(data); //data will be containing the vote count which you have echoed from the controller

                }
            });

在控制器中

$data = $_POST['data'];  //$data will contain the_id
//do some processing
echo "yourVoteCount";

澄清

我觉得你很困惑

{data:the_id}

success:function(data){

为了您自己的清晰度,两个数据都是不同的,您可以将其修改为

success:function(vote_count){
$(span#someId).html(vote_count);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读