weixin_33743880 2017-06-22 13:33 采纳率: 0%
浏览 152

将php数组传递给ajax

I have this array:

    $users = array();
// Finally, loop over the results
foreach( $select as $email => $ip )
{
    $users[$email] = $ip;
}

Now i want to pass this array to ajax and print in another script:

$html="<center><a href=\"#\" id=\"check_spam\" onclick=\"$.ajax({type: 'POST', url: 'mcheck.php', data:'users=$users', success: function(data){ $('#results').html(data);}});\">Check users for spam</a></center>";

mcheck.php

echo $_POST['users'];

This code does not work. How can i do this?

  • 写回答

3条回答 默认 最新

  • 撒拉嘿哟木头 2017-06-22 13:39
    关注

    The Best way is to encode array into JSON format.

    In PHP:

    $users_json = json_encode($users);
    

    // To decode in javascript

    var obj = JSON.parse(json_str);
    
    评论

报告相同问题?