Jump to content

PHP Header Error in different environment


shades

Recommended Posts

Hi Guys,

 

I ran the below code on my laptop and it was working fine and I used Chrome and WAMP for the development. But when I uploaded the file on the server and tested it gave the below error. I tried including ob_start(), and ob_end_flush(); but it did not work, so could someone point out the mistake in code??

 

Error: Cannot modify header information - headers already sent by index.php

 

Code: 

<div class="maincolumnheader">                    
                    
    <span class="btn btn-primary">Groups</span>
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-primary">Dimensions</a>                    
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-primary">Vignettes</a>
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-success">Illogical Cases</a>
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-primary">Random Sample</a>
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-primary">Decks</a>
    <Span><img src="/images/arrow.jpg" class="arrow" alt=">>"></Span>
    <a class="btn btn-primary">Export</a>
                    
</div>

<div class="maincolumnbody" >
  <?php  
     include("dbconnect.php");
     if(isset($_POST["submit"]))  
     {  
          if(!empty($_POST["search"]))  
          {  
               // $query = str_replace(" ", "+", $_POST["search"]);
               $query = implode("+", $_POST["search"]);
               header("location:index.php?page=removevigtext&groupname=".$_GET["groupname"]."&search=" . $query);  
          }  
     }  
  ?>  
    
  <form class="form-horizontal" method="post">  
      <div class="panel panel-default">
        <div class="panel-heading"><h4>Select the Dimension Level/s</h4></div>
        <div class="panel-body">
        <div class="row">
        <div class="col-xs-6 col-lg-6">
          <?php

            $dimgroupname = $_GET["groupname"];
            $stmtcount = $dbconnect -> prepare("SELECT COUNT(DISTINCT dimid) as dcount FROM dimensionlevel l INNER JOIN dimensiongroup g ON g.groupid = l.dimgroupid WHERE g.groupname = :groupname");
            $stmtcount -> bindValue(':groupname',$dimgroupname);
            $stmtcount -> execute();
            $rowcount = $stmtcount -> fetch();
            if ($rowcount['dcount']==0){
              echo "No level texts are avaialable for selection!!!";
            }
            else {
            for ($i=1; $i <= $rowcount['dcount']; $i++){
                  // echo  '<label for="search['.$i.']">Select Level'.$i.' text &nbsp</label>';
                  echo  '<label for="search['.$i.']">Select Level &nbsp<select name="search['.$i.']">';
                  echo '<option value=""> </option>';

                  $stmt = $dbconnect -> prepare ("SELECT dimlevelvalue FROM dimensionlevel l INNER JOIN dimensiongroup g ON g.groupid = l.dimgroupid WHERE g.groupname = :groupname");
                  $stmt -> bindValue(':groupname',$dimgroupname);
                  $stmt -> execute(); 

                  while ($row = $stmt->fetch()) {
                   
                    echo '<option value="'.$row['dimlevelvalue'].'">'.$row['dimlevelvalue'].'</option>';
                  }                
                          
                  echo  '</select></label>';
                  echo  '<br>';
            }
          }
         ?> 
         </div>
         </div>
        </div>
      </div>
          

      <input type="submit" name="submit" class="btn btn-info" value="Search" />  
  </form>  
  <br />
  <div class="cspan">
  </div>
  <form class="form form-horizontal" name="removevig_form" id="removevig_form" >  
    <table id="maintable2" class="table table-striped table-bordered">
      <thead id="table2head">
        <tr>
          
          <th class="textview" id="table2head1"><label for="masterid">Select All &nbsp</label><input type="checkbox" id="masterid"></th>
          <th class="textview" id="table2head2">Vignettes</th>
        </tr>
      </thead>
      <tbody id="table2body"> 
       <?php  
         if(isset($_GET["search"]))  
         {  
            $dimgroupname = $_GET["groupname"];
            $keywords = explode(" ", $_GET["search"]);  
            $totalKeywords = count($keywords);
            $sqlquery = "SELECT * FROM vignettetext vig INNER JOIN dimensiongroup dim ON dim.groupid = vig.groupid WHERE dim.groupname = ?  AND vigtext LIKE binary ? "; //use binary for exact match
           // dim.groupname = ?  AND 
            for($i=1 ; $i < $totalKeywords; $i++){
                $sqlquery .= " AND vigtext LIKE binary ? ";
              }

            $stmt=$dbconnect->prepare($sqlquery);
            $stmt->bindValue(1, $dimgroupname);
            foreach($keywords as $key => $keyword){
                $stmt->bindValue($key+2, '%'.$keyword.'%');
              }
            
            $stmt->execute ();
            $count=1;
           while($row = $stmt -> fetch())  
           {  
                echo '<tr><td><label for="id['. $row["id"].']">'.$count.'&nbsp</label><input type="checkbox" class="sub_chk" id="id['. $row["id"].']" value="'. $row["id"].'" ></td><td>'.$row["vigtext"].'</td></tr>';
                $count++;
           }           
         }  
       ?> 

      </tbody>
     </table> 

    <div class="row">
        <div class="col-xs-6 col-lg-6">
            <a href="/index.php?page=dimvigview&groupname=<?php echo $_GET["groupname"]; ?>"><input type="button" class="btn goback" name="goback" value="Back"/></a>
        </div>

        <div class="col-lg-6 submitbtn">          
            <a data-popup-open="popup-1" href="#">
            <input type="button" class="btn btn-primary" name="remvig" id="remvig" value="Remove Vignettes" />
            <a href="/index.php?page=vigrandomsample&groupname=<?php echo $_GET["groupname"]; ?>"><input type="button" name="vigrandom" id="vigrandom" class="btn btn-primary" value="Random Sample"/></a>
            </a>
            <div class="popup" data-popup="popup-1">
                <div class="popup-inner">
                    <h3>Are you sure you want to delete the texts ??</h3>
                    <a data-popup-close="popup-1" href="#"><input type="button" class="btn btn-danger" value="NO" /></a>
                    <a href="/index.php?page=removevigtext&groupname=<?php echo $_GET["groupname"]; ?>"><input type="button" name="removevig" id="removevig" class="btn btn-primary" value="Yes"/></a>
                    <a class="popup-close" data-popup-close="popup-1" href="#">x</a>
                </div>
            </div>
        </div>
      </div>
  </form> 
</div>    
<span id="error_message" class="text-danger"><span class="glyphicons glyphicons-ok"></span></span>
<span id="success_message" class="text-success"></span>   

Thank you.

Link to comment
Share on other sites

You cannot use header() if there has been output.

 

Output buffering is not the best answer. Refactor your code so that you make all the important decisions, like how to handle form input and redirections, before you produce any output. At its simplest,

<?php

if (form was submitted) {
	process form
	redirect
}

?>
output
Also remember that header() will not stop the script. If you want the script to stop (and you do) then exit; right after.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.