i have live file reading and i'm getting the files name from many links
name_container="<div><a class=\"friendclick\" data-frclick=\"$fr\" href=\"#\">$fullname</a></div>";
i need when (data-frclick) change -it changes on click- i need to pass it's value (targetread=dat) as (dat)
$('.friendclick').click(function(){
var dat=$(this).data('frclick');
});
i tried to put comet() function inside the click event function , it worked only when i click the link ,
but i couldn't get any live update from the file automatically, maybe if you can help me with making the (dat) global variable ,or any other suggestions !
$('.friendclick').click(function(){
dat=$(this).data('frclick');
var timestamp = null; //read
function comet() {
$.ajax({
type : 'Get',
url : 'includes/read.php?timestamp=' + timestamp+'&targetread='+dat,
async : true,
cache : false,
success : function(data) {
var json = eval('(' + data + ')');
if(json['msg'] == ''){
$('#msg').html('No msg');
}else {
$('#msg').html(json['msg']);
$('#msg').animate({scrollTop:$('#msg').get(0).scrollHeight},200);
}
timestamp = json['timestamp'];
setTimeout('comet()', 1000);
},
error : function(XMLHttpRequest, textstatus, error) {
//alert(error);
setTimeout('comet()', 65000);
}
});
}
$(function() { // write
comet();
$('#send').bind('keyup', function(e) {
var msg = $(this).val();
if(e.keyCode == 13 && e.shiftKey) {
return ;
}else if(msg!='' && e.keyCode == 13) {
$.ajax({
type : 'GET',
url : 'includes/write.php?msg='+ msg.replace(/\n/g,'<br />')+'&target='+dat,
async : true,
cache : false
});
$(this).val('')
}
});
});
});
and the php page for reading the file $target=$_GET['targetread']; here i got the target file then from the DB i get $file_name
$filename = 'messaging/'.$file_name.'.txt';
$last = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$current = filemtime($filename);
while( $current <= $last) {
usleep(100000);
clearstatcache();
$current = filemtime($filename);
}
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $current;
echo json_encode($response);