I am stuck in a weird problem I am using ajax call inside setInterval() but it's not working. Please help
Javascript:
$(document).ready(function(){
var limit = 25;
var start = 0;
var action = 'inactive';
var to_id = "<?php echo $_GET['id']; ?>";
setInterval(function load_messages(limit,start,to_id)
{
$.ajax({
url : "fetch-messages.php",
method : "POST",
data : {limit:limit,start:start,to_id:to_id},
cache : false,
success :
function(data)
{
$('#message_body').append(data);
if(data=='')
action = 'active';
else
action = 'inactive';
}
});
},1000);
if(action == 'inactive'){
action = 'active';
load_messages(limit,start,to_id);
}
var height_load = ($('#message_body').height());
$('#message_body').scroll(function(){
if(($('#message_body').scrollTop())>height_load && (action=="inactive")){
height_load=height_load*1.5;
action = 'active';
start = start + limit;
setTimeout(function(){
load_messages(limit,start,to_id)
},100);
}
});
});
This problem could be specific to me but I really need help. The above code is written in message.php. which also has a division id="message_body"
And the code for Fetch-message.php is below:
<?php
if(isset($_POST["limit"],$_POST["start"],$_POST["to_id"])){
$dbc = mysqli_connect($hn,$un,$pd,$db);
$start = $_POST["start"];
$limit = $_POST["limit"];
$to_id=$_POST["to_id"];
$from_id = $uid;
$to_id_details = mysqli_fetch_assoc(mysqli_query($dbc,"SELECT * FROM public_signup WHERE id='$to_id'"));
$message_sent_number=mysqli_num_rows(mysqli_query($dbc,"SELECT * FROM message WHERE to_id='$to_id' and from_id='$from_id'"));
$message_recieved_number=mysqli_num_rows(mysqli_query($dbc,"SELECT * FROM message WHERE to_id='$from_id' and from_id='$to_id'"));
if($message_sent_number!=0 || $message_recieved_number!=0){
$query = "SELECT * FROM message ORDER BY id DESC LIMIT $start, $limit";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result)){
$db_to_id = $row['to_id'];
$db_from_id = $row['from_id'];
if($to_id==$db_to_id && $from_id==$db_from_id){
echo '<div id="new_line"><div class="individual_message_reciever">
<p><span id="message_name">'.ucfirst($first_name)." ".ucfirst($last_name).':</span>
'.nl2br($row['message']).'</p>
</div></div>';
} else if($from_id==$db_to_id && $to_id==$db_from_id){
echo '<div id="new_line"><div class="individual_message_sender">
<p><span id="message_name">'.ucfirst($to_id_details['first_name'])." ".ucfirst($to_id_details['last_name']).':</span>
'.nl2br($row['message']).'</p>
</div></div>';
}
}
}
}
?>
I am getting error in console ReferenceError: load_messages is not defined Please help. THANKS!!