0

Edit: Solved by reading BETTER the answer from mario at How to fix "Headers already sent" error in PHP. Adding an ob_start() at the beginning of my file solved my issues.

Recently I figured out a very strange behavior in php: when certain lines of javascript are present php throws "Cannot modify header information - headers already sent..." error.

I read tons of posts saying it is impossible to modify header information with header() function, but I always been able to!

The code is:

if(this.innerHTML == "menu"){
    document.getElementById("side_panel").style.left = "0px";
    document.getElementById("side_panel").style.paddingLeft = "5vw";
    this.style.opacity = "0";
    setTimeout(function(){document.getElementById("nav_button").innerHTML = "close";document.getElementById("nav_button").style.opacity = "1"},200);
}
else {
    document.getElementById("side_panel").style.left = "-250px";
    document.getElementById("side_panel").style.paddingLeft = "0vw";
    this.style.opacity = "0";
    setTimeout(function(){document.getElementById("nav_button").innerHTML = "menu";document.getElementById("nav_button").style.opacity = "1"},200);
}

It works when I keep only:

if(this.innerHTML == "menu"){}

Just adding this:

if(this.innerHTML == "menu"){
}
else{
}

Make php throw the error.

Please help me, I'm hopeless. Thank you all for your amazing work.

  • to be honest, there is no way javascript can affect the headers sent by PHP - since PHP runs on the server and javascript runs in the browser after PHP has sent the whole page to the browser – Bravo Oct 19 '19 at 23:46
  • I think the problem is that javascript modifies the page before php can change the header, so the php throws the error – Lorenzo Bodini Oct 21 '19 at 09:54

0 Answers0