PHP included files not loading, possible javascript issue

I have a countdown timer in javascript which finally works after some tinkering, but one problem still persists. I have it set to reload the index.php page when the counter gets to zero, which it does, but for some reason it does not load any of my included php files like my CSS or PDO connection files.

include(“include/connect.php”);
include(“include/define.php”);
include(“header.php”);

This is a stupid work-around, but if I have the javascript redirect to test.php and then test.php redirects back to index.php it works, but I’d rather have this working properly. The javascript is below. I have tried various ways of redirecting but the same thing happens.

Thanks (I hope this is OK in the PHP group)

if(localStorage.getItem("count_timer")){
    var count_timer = localStorage.getItem("count_timer");
} else {
    var count_timer = 60;
}
var minutes = parseInt(count_timer/60);
var seconds = parseInt(count_timer%60);
function countDownTimer(){
    if(seconds < 10){
        seconds= "0"+ seconds ;
    }if(minutes < 10){
        minutes= "0"+ minutes ;
    }
	
	function Redirect() {
	window.location = "test.php";
	 }	
    
    document.getElementById("total-time-left").innerHTML = minutes+":"+seconds;
    if(count_timer <= 0){
         localStorage.clear("count_timer");
		setTimeout(function() {
		Redirect();
		 }, 100);		 
		
    } else {
        count_timer = count_timer -1 ;
        minutes = parseInt(count_timer/60);
        seconds = parseInt(count_timer%60);
        localStorage.setItem("count_timer",count_timer);
        setTimeout("countDownTimer()",1000);
    }
}
setTimeout("countDownTimer()",1000);

How do you know the included files are not being loaded? What symptom or error are you getting?

You must have some initial condition (assumption), conditional logic, or even a redirect (redirect loop) in the index.php code that’s causing the problem.

You will need to post enough of the index.php code that reproduces the problem in order to get any specific help.

You are showing the redirect to test.php, which you claim is working.

How does your reloading of the index.php look like?
Be aware that setting the window.location to the same value that it already has will NOT trigger a reload of zhe page.

You need to use reload() method instead.

If you are doing that already, it may as well be a cache issue leading to the page is served from browser cache instead of running the php script.

Cheers

Problem fixed, it was due to a redirect of the page in question. Note: I could see the included files were not being included as they contained my header page with links to all the CSS and Javascript files, and as there was no formatting on the page I could tell they were not included.

If you are composing output from different files, think about using a template engine like Blade or Twig or Volt etc…

While it is technically valid and practical for small pages to mix business logic with output in the same file, this can become a big noodlesoup when your page becomes larger.

Even if you are not using a template engine, the included file should not generate any output on its own, but rather return it to your main script. This gives you way more control about what is getting send to the browser and what is not.

Sponsor our Newsletter | Privacy Policy | Terms of Service