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中时implodequery它给了我数组,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;
4

2 回答 2

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']));

编辑 看看这篇文章,它会很有用。

希望能帮助到你。

于 2017-03-12T10:45:50.263 回答
0

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

query.join(',');

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

所以这就是我所做的:

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

于 2017-03-12T12:13:46.727 回答