0

Hi I've searched for solutions but I can't fixed it -_-

I've got a form, when I click submit it shows this error

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\new1v2\topnav.php:72) in C:\xampp\htdocs\new1v2\alumnireg.php on line 68

This is a part of my php code

if (mysqli_query($conn, $sql) === TRUE) {
  echo 
  header('location:thanks.php');

where if I click submit it should go to thanks.php but if I remove my topnav.php It functions well

What is the problem with my topnav.php?

<!-- active class in navigation -->
<style>
  .active {
  background-color:#2980b9;
  }
</style>
<!-- end of active -->
<style>
  .nav li a:hover{
    background-color: #0452BC;
   /*font-size:18px;*/
 }
</style>

<script>
// Change style of top container on scroll

window.onscroll = function() {scrollFunc()};
function scrollFunc() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
    document.getElementById("myTop").classList.add("w3-blue","w3-card-4");
    document.getElementById("myIntro").classList.add("w3-show-inline-block");
} else {
    document.getElementById("myIntro").classList.remove("w3-show-inline-block");
    document.getElementById("myTop").classList.remove("w3-blue","w3-card-4");
}
}


</script>
<!-- for top nav to be not overlapped by the indicators of carousel -->

<style>
  .w3-card-4.w3-blue{z-index: 99999;}
</style>
<!-- end of style for top nav to be not overlapped -->

<div id="myTop" class="w3-top w3-container w3-padding-2 w3-theme w3-large">
  <!-- <i class="w3-opennav w3-hide-large w3-xlarge w3-margin-left w3-margin-right" onclick="w3_open()"></i> -->
  <div id="myIntro" class="w3-hide">
  <a href="index.php">
   <img src="admin/img/fcuautonomous.png" width="200px" height="50" style="padding:5px;">
  </a>
  </div>
</div>

<header class="w3-container w3-theme w3-padding-18 w3-blue" style="background-image: url(admin/img/top.jpg); background-size: 100% 100%;">
  <h1 class="w3-xxxlarge w3-padding-16">
   <a href="index.php">
   <img src="admin/img/fcuautonomous.png" width="400px">
  </a>
  </h1>
</header>

<nav class="w3-card-4" style="background-color: #013275;">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <i class="fa fa-chevron-circle-down" aria-hidden="true"></i>                       
      </button>
      <!-- <a class="navbar-brand" href="index.php"><b><img src="admin/img/fculogo.png" height="35" width="35"></b></a> -->
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-right">

        <li><a id='homie' href="index.php"><font color="white">Home</font></a></li>
        <li><a href="alumnireg.php"><font color="white">Alumni Registration</font></a></li>
        <li><a href="news_events.php"><font color="white">Events</font></a></li>
        <!-- <li><a href="job_opp.php"><font color="white">Job Oppurtunities</font></a></li> -->
        <li><a href="featured_alum.php"><font color="white">Featured Alumni</font></a></li>
        <li><a href="#contact"><font color="white">Photo Gallery</font></a></li>

      </ul>
    </div>
  </div>
</nav>

<br><br>

Please help, it's for my THESIS

LecheDeCrema
  • 392
  • 5
  • 21

3 Answers3

4

You could use buffering.

Try the following:

ob_start(); // <-- put this at the very top of the script, after opening PHP tags. 

if (mysqli_query($conn, $sql) === TRUE) { 
    header('location:thanks.php'); 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close();


ob_end_flush(); 
//flush();
//exit(0);

In case you want avoid errors for any other reason, use line below:

if (mysqli_query($conn, $sql) === TRUE && headers_sent() === false) {...

Sources:

http://php.net/manual/en/function.headers-sent.php

http://php.net/manual/en/ref.outcontrol.php

Nitin
  • 898
  • 1
  • 9
  • 25
1

The base problem is that you're echoing out some output (style tags, script tags, etc) and then trying to set and send the headers. So either you can move the logic on top of the include of the topnav.php or you solve it using other of the options.

Buffering (ob_start/end) captures the output and only echo (flush) it once you call the ob_end_flush.

The other option is just to move the if block on top of the include(topnav.php)

LordNeo
  • 1,195
  • 1
  • 8
  • 21
1

My problem got solved using this:

Anytime I get this error I try this to solve this using an automated PHP tool

Download Link: phptags

phptags -v -u -w -W -a  myfile.php