如何将所有动态id的arrey值获取到javascript VAR CID,以便在sql中找到匹配项


How to get all dynamic ids arrey value to javascript VAR CID to find match in sql

在我的php代码中,我有每个帖子的动态php$id,就像在我的index.php页面中一样,<div id=post'.$id.'>在浏览器控制台上显示为

<div id=post11>

<div id=post12>

<div id=post13>

等等。

现在我想在我的server.php文件中发送我上面的帖子id,以便通过id进行查询(如果sql中有任何id匹配),这些id将通过Javascript VAR CID = ''从index.php页面发送。

所以我的我想要我的javascript发送的数据像

VAR CID = 11 or 12 or 13 or anything else to match in sql;

我的server.php文件通过获取这些id

$_REQUEST['cid']= 11 or 12 or 13 or others more else to match in sql;

然后查询sql 中是否有任何id匹配

WHERE id =".$_REQUEST['cid']." AND page_id=".$_REQUEST['tutid']." ORDER BY id DESC

以下是我的相关脚本:更新脚本(A server and Clint side regular update script):

<script type="text/javascript" charset="utf-8">
var CID = ''; // Post id
var tutid = '<?php echo $pageid; ?>'; // Particular page id
function addrep(type, msg){
$("#newreply"+CID).append("");
}
function waitForRep(){
    $.ajax({
        type: "GET",
        url: "server.php",
        cache: false,
        data: "tutid="+ tutid + "&cid="+ CID,
        timeout:15000, 
        success: function(data){ 
            addrep("postreply", data);
                setTimeout(
                waitForRep, 
                15000 
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(
                waitForRep, 
                15000 
            );
        }
    });
};
$(document).ready(function(){
    waitForRep();
});
</script>

index.php

echo '<div id=post'.$id.'>name: AAAA </div>';
echo '<div id=post'.$id.'>name: BBBB </div>';
echo '<div id=post'.$id.'>name: CCCC </div>';

服务器.php

while (true) {
   if($_REQUEST['tutid'] && $_REQUEST['cid']){
   // make query by $tutid and $cid
   }
}

添加data-post-id="'.$id.'"属性执行Index.php中的div(这样就不必从id属性中提取ID):

echo '<div data-post-id="'.$id.'" id=post'.$id.'>name: '.$name.' say: Detail: '.$datail.' </div>';

脚本:

var tutid = '<?php echo $pageid; ?>'; // Particular page id
// --- to get all ID's as array: --- //
var CID = [];
$('div[data-post-id]').each(function(i){
    CID[i] = $(this).data('post-id');
});
// --- to get single ID: --- //
// var CID = $('div[data-post-id]').data('post-id');
function addrep(type, msg){
    // --- if CID is an array, this ain't gonna work: --- //
    //$("#newreply"+CID).append("");
    // --- you need to loop through the CID array: --- //
    CID.forEach(function(id){
        $("#newreply"+id).append("");
    });
}
function waitForRep(){
    $.ajax({
        type: "GET",
        url: "server.php",
        cache: false,
        data: {
            tutid : tutid,
            // this way array containing all ID's can be sent:
            cid : CID
        },
        timeout:15000, 
        success: function(data){ 
            addrep("postreply", data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
        }
    });
};

然后在server.php中:

// need to loop through the $_GET['cid'], as it's an array sent over AJAX:
foreach($_GET['cid'] as $key => $value){
    // do something with $value ...
}