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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6616539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:51:30  来源:igfitidea点击:

post ajax data to PHP and return data

phpjquerycodeigniterpost

提问by billa

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:

如何将一些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.

                }
            });

回答by Rafay

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

                }
            });

in the controller

在控制器中

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

Clarification

澄清

i think you are confusing

我觉得你很困惑

{data:the_id}

with

success:function(data){

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

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

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

回答by Marc B

For the JS, try

对于JS,尝试

data: {id: the_id}
...
success: function(data) {
        alert('the server returned ' + data;
    }

and

$the_id = intval($_POST['id']);

in PHP

在 PHP 中

回答by serialworm

So what does count_votes look like? Is it a script? Anything that you want to get back from an ajax call can be retrieved using a simple echo (of course you could use JSON or xml, but for this simple example you would just need to output something in count_votes.php like:

那么 count_votes 是什么样子的呢?是脚本吗?您想从 ajax 调用中返回的任何内容都可以使用简单的 echo 来检索(当然您可以使用 JSON 或 xml,但对于这个简单的示例,您只需要在 count_votes.php 中输出一些内容,例如:

$id = $_POST['id'];

function getVotes($id){
    // call your database here
    $query = ("SELECT votes FROM poll WHERE ID = $id");
    $result = @mysql_query($query);
    $row = mysql_fetch_row($result);

    return $row->votes;
}
$votes = getVotes($id);
echo $votes;

This is just pseudocode, but should give you the idea. What ever you echo from count_votes will be what is returned to "data" in your ajax call.

这只是伪代码,但应该给你一个想法。您从 count_votes 回显的内容将是您的 ajax 调用中返回到“数据”的内容。