使用Ajax将数组发送到PHP脚本和Implode

时间:2017-03-12 10:30:35

标签: php ajax php-7.1

这是我的 Ajax POST请求:

function getRelated() {
    var elements = (document.getElementsByClassName('escashare'));
    var query = [];
    for(var i=0;typeof(elements[i])!='undefined';query.push(elements[i++].getAttribute('data-id')));

    $.ajax({
        type: "POST",
        url: baseUrl+"/requests/get_related.php",
        data: "query="+query+'&_token='+_token, 
        cache: false,

        success: function(html){
            $('#main-content').append(html);
        }
    });
}

所以基本上我POST有一组int个数字,如:

["326", "311", "312", "313", "314", "316", "317", "318", "319", "15", "9", "87"]

在我的PHP implode query数组中,它给了我NULL,但为什么?

$newQuery = implode(',', $QueryFromPost);
var_dump($newQuery); //NULL

修改

我需要将它用于:

$query = $this->db->query(sprintf("SELECT * FROM `posts` WHERE `id` IN ('%s')", $newQuery));

while($result = $query->fetch_assoc()) {
    $rows[] = $result;
}

if(!empty($rows)) {
    foreach($rows as $row) {
        $output .= '<div class="stage">'.$row['id'].'</div>';
    }
}

return $output;

2 个答案:

答案 0 :(得分:0)

    function getRelated() {
      var elements = (document.getElementsByClassName('escashare'));
      var query = [];
      for(var i=0;typeof(elements[i])!='undefined';query.push(elements[i++].getAttribute('data-id')));
   // Right here you can implode the array like this :
    query = query.join();// this is now a string containing your data
    // you can then pass it in your query string 

    var jsonString = JSON.stringify(query);

    $.ajax({
    type: "POST",
    url: baseUrl+"/requests/get_related.php",
    data: {data : jsonString,token: token}, 
    success: function(html){
        $('#main-content').append(html);
    }
   });
  }

在服务器端代码中,您将使用explodejson_decode函数从查询字符串中提取数据。

$data = explode(",", json_decode(stripslashes($_POST['data']));
foreach($data as $d){
    echo $d;
 }
 $token = json_decode(stripslashes($_POST['token']));

修改 看看这个post,它会很有用。

希望它有所帮助。

答案 1 :(得分:0)

首先在我的JS函数中,我忘了:

query.join(',');

接下来在我的PHP中,我忘记从我的MYSQL查询中删除单引号,而且我根本不需要使用内爆。

所以这就是我所做的:

JS 功能:

function getRelated() {
    var elements = (document.getElementsByClassName('escashare'));
    var query = [];
    for(var i=0;typeof(elements[i])!='undefined';query.push(elements[i++].getAttribute('data-id')));
    query = query.join(',');

    $.ajax({
        type: "POST",
        url: baseUrl+"/requests/get_related.php",
        data: "query="+query+'&_token='+_token, 
        cache: false,

        success: function(html){
            $('#main-content').append(html);
        }
    });
}

PHP 功能:

function getRelated($get_query) {

    $query = $this->db->query(sprintf("SELECT * FROM `posts` WHERE `id` IN (%s)", $this->db->real_escape_string($get_query)));

    while($result = $query->fetch_assoc()) {
        $rows[] = $result;
    }

    if(!empty($rows)) {
        foreach($rows as $row) {
            $output .= '<div class="stage">'.$row['id'].'</div>';
        }
    }

    return $output;
}

Ayush 的信用!