I have a page that calls a PHP script every 4 seconds with Ajax. The PHP script echoes out some text which is then put in a div on the page. (The PHP script does connect to a MySQL server, could that be the reason for my problems below?)
Here's the strange part. The first time the PHP script is called, it echoes all the text fine. However after 4 seconds, when Ajax calls the PHP script again, the page becomes blank. To make things even more strange, when I right-click View Source on MS Edge, all of the page content loads in, and then if I close the Source Code, after 4 seconds it disappears again.
Does anyone know why this is happening?
My JS file with Ajax:
function updateCandles(){
var candleIndexStart = $('#candle-index-start').text();
var candleSort = $('#candle-sort').text();
var resultArray = $.ajax({
type: 'GET',
url: 'php/display_candles.php',
data: {
'sort': candleSort,
'candle-start': candleIndexStart
},
async: false
}).responseText.split("h8ktncuidabciafvabc7932hcueqbc73gd8713gdy713gd6831df8136fd13d");
$('.candles').html(resultArray[0]);
$('#candle-counter').html(resultArray[1]);
setTimeout(updateCandles, 4000);
}
$(document).ready(function(){
updateCandles();
});
My PHP Script:
<?php
session_start();
function timeAgo($time_ago)
{
$time_ago = strtotime($time_ago);
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
return "just now";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
return "one minute ago";
}
else{
return "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
return "an hour ago";
}else{
return "$hours hrs ago";
}
}
//Days
else if($days <= 7){
if($days==1){
return "yesterday";
}else{
return "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
return "a week ago";
}else{
return "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
return "a month ago";
}else{
return "$months months ago";
}
}
//Years
else{
if($years==1){
return "one year ago";
}else{
return "$years years ago";
}
}
}
$connection = mysqli_connect("localhost", "username", "pass", "db");
if(!$connection) {
echo "Error";
}
if($_GET['teacher'] == 'yes'){
$result2 = mysqli_query($connection, "SELECT * FROM candles WHERE approved=0 ORDER BY date desc LIMIT " . $_GET['candle-start'] . ",20");
} else {
$result2 = mysqli_query($connection, "SELECT * FROM candles WHERE approved=1 ORDER BY date " . $_GET['sort'] . " LIMIT " . $_GET['candle-start'] . ",20");
}
if(mysqli_num_rows($result2) > 0) {
$counter = 0;
while($row = mysqli_fetch_assoc($result2)) {
$counter = $counter + 1;
if($counter % 2 == 1){
echo "</div>";
echo "<div class='candle-row'>";
}
if($row["approved"] == 1 and $_GET['teacher'] == 'yes') {
continue;
}
echo "<div class='candle'><div class='candle-icon'>";
echo '<span class="fa-stack fa-4x"><i class="fa fa-square-o fa-stack-2x"></i><i class="fa fa-' . $row["candle_icon_type"] . ' fa-stack-1x" style="color: ' . $row["colour"] . '"></i></span></div>';
echo '<div style="display:table;height:100%;"><div style="display:table-cell;vertical-align:middle;"><div>';
echo "<h3><a href='candle.php?id=" . $row["id"] . "'>For " . ucwords($row["title"]) . "</a></h3>";
echo "<br>";
$result3 = mysqli_query($connection, "SELECT * FROM users WHERE id=" . $row['user_id'] . " LIMIT 1");
if(mysqli_num_rows($result3) > 0) {
while($row1 = mysqli_fetch_assoc($result3)) {
if($row["anonymous"] == 1 and $_SESSION["user_group"] == '1'){
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by an Anonymous User<br>";
} else {
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by " . $row1["username"] . "<br>";
}
}
} else {
echo "<h4><a href='candle.php?id=" . $row["id"] . "'>Shared by Unknown<br>";
}
echo "Shared " . ucwords(timeAgo($row["date"])) . "<br>" . $row['fuel'] . " Fuel</a></h4></div></div></div>";
echo "</div>";
}
}
echo "h8ktncuidabciafvabc7932hcueqbc73gd8713gdy713gd6831df8136fd13d";
if($_GET['teacher']) {
$result1 = mysqli_query($connection, "SELECT id FROM candles WHERE approved=0");
} else {
$result1 = mysqli_query($connection, "SELECT id FROM candles WHERE approved=1");
}
if(mysqli_num_rows($result1) == 0){
echo '0 Candles';
} elseif(mysqli_num_rows($result1) == 1){
echo '1 Candle';
} else {
echo mysqli_num_rows($result1) . ' Candles';
}
mysqli_close($connection);
?>
yes, EDGE
does seem to miss the console
object when you don't have it opened (the DOM Explorer as you called it). when your console
is closed, it doesn't "exist", so EDGE
throws an error which you can't see because your console
is closed.
(sounds weird to you? welcome to Microsoft
created logic)!
I guess you are using the console.log()
method in you code. so either you remove your console.log()
calls or you put this workaround to the top of your javascript:
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };