将带有 Ajax 的数组发送到 PHP 脚本并内爆
Send array with Ajax to PHP script and Implode
这是我的AjaxPOST
请求:
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;
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);
}
});
}
在服务器端代码中,您将使用 explode
和 json_decode
函数从查询字符串中提取数据。
$data = explode(",", json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
$token = json_decode(stripslashes($_POST['token']));
编辑
看看这个post,会有用的
希望对您有所帮助。
首先在我的 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!
这是我的AjaxPOST
请求:
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;
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);
}
});
}
在服务器端代码中,您将使用 explode
和 json_decode
函数从查询字符串中提取数据。
$data = explode(",", json_decode(stripslashes($_POST['data']));
foreach($data as $d){
echo $d;
}
$token = json_decode(stripslashes($_POST['token']));
编辑 看看这个post,会有用的
希望对您有所帮助。
首先在我的 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!