将ajax数据发布到PHP并返回数据 [英] post ajax data to PHP and return data

查看:66
本文介绍了将ajax数据发布到PHP并返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一些Ajax数据发布到控制器功能并将其取回?因为我想将一个整数发布到函数中,并获得另一个整数(发布ID的项目的总票数),并且在成功时我想回显该投票数. 我不知道如何将"id"发布到控制器功能.请查看我的代码:

How can i post some ajax data to the controller function and get it back? For I want to post one integer to the function, and get another integer (total votes for the item which ID is posted), and on success i want to echo that vote count. I dont know how can i post the "id" to the controller function. Please see my code:

//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},
            url: "http://localhost/test/index.php/data/count_votes",
            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){

data都不同,为了您自己的清楚起见,您可以将其修改为

both the data are different for your own clarity sake you can modify it as

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

这篇关于将ajax数据发布到PHP并返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆