简体   繁体   English

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

[英]Send array with Ajax to PHP script and Implode

This is my Ajax POST request: 这是我的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);
        }
    });
}

So basically I POST an array of int numbers like: 所以基本上我POST数组int相同的数字:

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

When in my PHP I implode the query array it gives me NULL but why? 在我的PHP中我implode query数组它给我NULL但为什么?

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

EDIT 编辑

I need to use it for: 我需要用它来:

$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;
    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);
    }
   });
  }

In your server side code , you will use explode and json_decode function to extract your data back from the query string . 在服务器端代码中,您将使用explodejson_decode函数从查询字符串中提取数据。

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

EDIT Take a look at this post , it will be useful. 编辑看看这篇文章 ,它会很有用。

Hope it helps. 希望能帮助到你。

First in my JS function I forgot to: 首先在我的JS函数中,我忘了:

query.join(',');

Next in my PHP I forgot to remove single quotes from my MYSQL query and also I don't need to use implode at all. 接下来在我的PHP中,我忘了从我的MYSQL查询中删除单引号,而且我根本不需要使用implode。

So here is what I did: 所以这就是我所做的:

JS function: 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: 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;
}

Credits to Ayush ! Ayush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM