0

In my php code I have dynamic php $id for each post like in my index.php page have <div id=post'.$id.'> display at browser console as

<div id=post11>

<div id=post12>

<div id=post13>

etc more else.

Now I want to sent my above post ids in my server.php file to make query by id (if any id match in sql) and those id will sent by Javascript VAR CID = '' from index.php page.

So my I want my javascript sent data like

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

And my server.php file get those id by

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

then make query to find if any id match in sql

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

Here is my related script: Update script (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>';

Server.php

while (true) {
   if($_REQUEST['tutid'] && $_REQUEST['cid']){

   // make query by $tutid and $cid

   }
}
koc
  • 955
  • 8
  • 26
  • Here's a link that can explain how to do it: http://stackoverflow.com/questions/247023/get-an-array-of-list-element-contents-in-jquery – Yair.R Mar 01 '15 at 11:54
  • @Yair.R how to get ids from here: `
    ` by `var arr = $("yourdivid").map(function() { return $(this).text() }).get();`
    – koc Mar 01 '15 at 12:03
  • return $(this).attr('id').slice(5) – Yair.R Mar 01 '15 at 12:05
  • Sorry I read about this before but, I can't do it. . – koc Mar 01 '15 at 12:11
  • update above, my script is a server and Clint side regular update script. so no need to click or others else. – koc Mar 01 '15 at 12:34

1 Answers1

0

Add data-post-id="'.$id.'" attribute do the DIV's in Index.php (so that you don't have to extract the ID's from id attribute):

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

Script:

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

Then in 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 ...
}
Artur Filipiak
  • 9,027
  • 4
  • 30
  • 56
  • Thank u sir. May be I can't described well. I want my server.php query get data where $cid= 10 or 12 or 13. If he found any data with any cid than he can do echo. Your code's `var CID = [];........CID[i] = $(this).data('post-id'); });` get only my 1st id And if i use `var CID = $('div[data-post-id]').data('post-id');` its do tothing. – koc Mar 01 '15 at 13:45
  • Sorry, I don't get your point. You may need to show your `server.php` code. And a more of `index.php` (at least what is the **newreply+ID** element) – Artur Filipiak Mar 01 '15 at 13:59
  • Sir. Array only get 1st post id, but I accept your ans because it give me a big guideline. – koc Mar 02 '15 at 10:57