从php传递一个返回值给js


Pass a return value from php to js

我有3个文件main.php, action.js和ajax.php,我成功地改变了一些div点击的内容从main.php到一些ajax.php在我的javascript文件中的ajax调用。它看起来像这样:

var value = $(this).attr("id");
$.ajax({
    type: 'get',
    url: "ajax.php",
    data: {
        auto_value: value
    },
    success: function(response) {
        $('.gridnr1, .textnr1').fadeOut(400, function(){
            $('.textnr2, .gridnr2').fadeIn(400);
        });
        var newtextcaption = $(response).filter('#newtextcaption').html();
        var box = $(response).filter('#box').html();
        $('#textnr2').html(newtextcaption);
        $('#gridnr2').html(box);
        for (var i=0; i<VALUE_FROM_AJAXphp; i++) {
            DO SOMETHING WITH i;
        }
    });

现在我需要一个返回值从一个函数在ajax.php在我的动作.js,因为我想迭代直到这个值(见上面的代码)。如何将这个值从ajax.php传递到action.js。我很困惑,我需要什么来获得价值ajax?, json ?还是别的什么?

谢谢。

成功函数response中的

是您从PHP返回的内容。所以发送JSON是最简单的,因为你的响应只是一个对象。

让ajax。php返回JSON

{
    "newtextcaption": "whatever the text is",
    "boxhtml": "whatever your box html is",
    "AjaxValue": 4389489473289
}

那么你的成功函数应该是

 success: function(response) {
                $('.gridnr1, .textnr1').fadeOut(400, function(){
                    $('.textnr2, .gridnr2').fadeIn(400);
                });
                var newtextcaption = response.newtextcaption;
                var box = response.boxhtml;
                $('#textnr2').html(newtextcaption);
                $('#gridnr2').html(box);
                for (var i=0; i<response.AjaxValue; i++) {
                     DO SOMETHING WITH i;
                }