如何将所有动态id的arrey值获取到javascript VAR CID,以便在sql中找到匹配项
在我的php代码中,每个帖子都有动态php$id,就像在我的index.php页面中一样,在浏览器控制台中显示为如何将所有动态id的arrey值获取到javascript VAR CID,以便在sql中找到匹配项,javascript,php,jquery,Javascript,Php,Jquery,在我的php代码中,每个帖子都有动态php$id,就像在我的index.php页面中一样,在浏览器控制台中显示为 等等 现在我想在我的server.php文件中发送我上面的帖子id,通过id进行查询(如果sql中有任何id匹配),这些id将通过JavascriptVAR CID=''从index.php页面发送 所以我希望我的javascript发送数据如下 VAR CID = 11 or 12 or 13 or anything else to match in sql; 我的serv
等等
现在我想在我的server.php文件中发送我上面的帖子id,通过id进行查询(如果sql中有任何id匹配),这些id将通过JavascriptVAR CID=''
从index.php页面发送
所以我希望我的javascript发送数据如下
VAR CID = 11 or 12 or 13 or anything else to match in sql;
我的server.php文件通过
$_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
以下是我的相关脚本:
更新脚本(服务器和客户端常规更新脚本)
:
添加data post id=“”.$id.”“
属性在Index.php中执行DIV(这样您就不必从id
属性中提取id):
这里有一个链接可以解释如何做:@Yair.R如何从这里获取ID:
byvar arr=$(“yourdivid”).map(函数(){return$(this.text()).get()代码>返回$(this).attr('id').slice(5)对不起,我以前读过这篇文章,但是我做不到。上面的更新,我的脚本是服务器端和客户端的常规更新脚本。因此,无需点击或其他。谢谢你,先生。也许我不能很好地描述。我希望我的server.php查询获得$cid=10、12或13的数据。如果他发现任何有cid的数据,他可以做回显。您的代码的var CID=[];。。。。。。。。CID[i]=$(this.data('post-id');})代码>仅获取我的第一个id,如果我使用var-CID=$('div[data-post-id')).data('post-id')对不起,我不明白你的意思。您可能需要显示server.php
代码。还有更多的index.php
(至少是newreply+ID元素)先生。数组只获得第一个帖子id,但我接受你的ans,因为它给了我一个大的指导。
<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>
echo '<div id=post'.$id.'>name: AAAA </div>';
echo '<div id=post'.$id.'>name: BBBB </div>';
echo '<div id=post'.$id.'>name: CCCC </div>';
while (true) {
if($_REQUEST['tutid'] && $_REQUEST['cid']){
// make query by $tutid and $cid
}
}
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){
}
});
};
// 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 ...
}