来回传递php和jquery之间的数组

时间:2014-07-31 19:37:15

标签: php jquery ajax

我正在制作一个通知系统,在该系统中,我从刚刚收到新通知的表中获取用户的行号。 我想将这些行号保存在一个数组中并将其传递给jquery,它调用了php来获取行号...它现在完美了我想把所有数组值的json返回对象传递到另一个php页面从一个数组中逐个获取值并对每个值执行一些操作.... 我怎样才能做到这一点 ?我搜索了很多但发现没有任何符合要求的东西。 谁能帮我?

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你在Javascript / JQuery中有一个JSON对象,你想再次通过Ajax将它发送给PHP。那你为什么不这样做呢:

在Javascript中:

function sendJSONToPHPAgain(jsonData)
    $.ajax({
        url: 'endpoint.php',
        data: {
            json: JSON.stringify(jsonData)
        },
        contentType: "application/json",
    });
}

在PHP(endpoint.php)中:

$json = json_decode($_REQUEST['json'], $assoc=true);
// now you have a nice php array to work with ...

例如,使用它:

var d = {a:2, b:3, c:"Hello World"};
sendJSONToPHPAgain(d);

或者我错过了什么?

答案 1 :(得分:1)

  $last_id = json_decode($_POST['arr'],true);
$size = sizeof($last_id);
for($i = 0; $i < $size; $i++)

{

   $element = $last_id[$i];
    $reply = mysql_query("select * from user_messages where id = '$element'");

        while($new_message = mysql_fetch_array($reply))
        {
            echo'
                    <div class="incomming_msg_wrapper">
                        <p class="incomming_msg">'.$new_message['message'].'</p>
                    </div> <br>
            ';
        }
}

**这就是我如何使它工作,它工作正常。我想做的就是通过$ .post()函数逐个访问json类型数组发送到php的元素**

答案 2 :(得分:0)

我找到了两个&#34; gists&#34;可能会帮助您:Convert array php to js arrayConvert json to php code

相关问题