1

I get a warning from my php file on the server not sure what is wrong. I am making an ajax call from my javascript function to the server and it just does not receive any response in xmlhttp.readyState == 4 && xmlhttp.status == 200.

I tried to make the same call to the php file/mysql database located on my local computer it works but it would not work for a remote host. Also, i have a similar php file on the server with just the select clause different and it works there not sure what is wrong here ?

.

Warning: Cannot modify header information - headers already sent by (output started at /home2/marshell/public_html/cfv/getuserpostbybusnumber.php:2) in /home2/marshell/public_html/cfv/getuserpostbybusnumber.php on line 4

.

<?php
ob_start();
header("Access-Control-Allow-Origin: *");
$q = intval($_GET['q']);



$con=mysqli_connect("localhost","ma","Ad","mars","3306");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM cfv_viewbusupdate WHERE  BusNumber = ".$q."   ORDER BY DatePosted DESC");

  while($row = mysqli_fetch_array($result))
  {

  echo "<P>";
  echo "<B><font color=\"3300CC\">#" . $row['DatePosted'] . "</font></B> --";
  echo "" . $row['username'] . " posted </br>";
  echo "<B>Bus Number . </B>";
  echo "<font color=\"CC0033\">" . $row['BusNumber'] . "</font></br><B> Going towards </B>";
  echo "<font color=\"CC0033\">" . $row['Direction'] . "</font></br> <B>Stop Name: </B>";
  echo "<font color=\"CC0033\">" . $row['StopNames'] ."</font></br><B> Time </B><font color=\"CC0033\">".$row['time']." </font></br><B> Status </B>";
  echo "<font color=\"CC0033\">" . $row['Status'] . "</font> ";
  echo "</br> <B> Comment's  </B>: <font color=\"CC0033\">" . $row['comments'] . "</font>";
  echo "</P>";
  echo "<hr> ";
  }

mysqli_close($con);

?>

.

function updateUserPost(str) {




    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {

         if (xmlhttp.readyState < 4) {
                 showModal();
              } 


        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {


            //  $('#select-choice-direction-foruserpost').html(xmlhttp.responseText).selectmenu( "refresh");
              hideModal();
             document.getElementById("result").innerHTML = xmlhttp.responseText;
            //alert(xmlhttp.responseText); 


        }
    }

    xmlhttp.open("GET", "http://www.xyz.uni.me/cfv/getuserpostbybusnumber.php?q="+str, true);


    xmlhttp.send();

}

dev_marshell08
  • 1,051
  • 3
  • 18
  • 40
  • put ob_start after the Header – ɹɐqʞɐ zoɹǝɟ Mar 24 '14 at 10:15
  • exactly which lines of your script are lines 2 and 4? Because I highly doubt `ob_start()` sends headers, and I doubt even more that `intval()` produces headers. – Tularis Mar 24 '14 at 10:15
  • @ferozakbar and how exactly would that solve the problem ??? `ob_start` just starts output-buffering. Headers are part of the output (just the first part). So starting the buffer later helps... how? – Tularis Mar 24 '14 at 10:16
  • 3
    And also check if there is any space before ` – PHP Avenger Mar 24 '14 at 10:16
  • is it the complete code or part of the code.sometimes when we break then between this the space will be considered as the output.so check your code entirely – ɹɐqʞɐ zoɹǝɟ Mar 24 '14 at 10:20
  • @NetSurgeon yes you are right the space was the problem and it works now. What i don't understand is why did it work on my local computer then ? I was expecting a similar behaviour since both the files were exactly the same. – dev_marshell08 Mar 24 '14 at 10:24
  • 1
    _“What i don't understand is why did it work on my local computer then ?”_ – maybe because that had output buffering enabled in the PHP configuration already. – CBroe Mar 24 '14 at 11:12
  • @CBroe Yes you are correct output buffering was enabled in the PHP configuration file. – dev_marshell08 Mar 24 '14 at 21:41

1 Answers1

1

This issue happens when there is white space, Most of the time its before the <?php or <? tag, some time its in one of the include files, mostly at the end of file. make sure you dont have those spaces. Error message shown will point you to that location.

I think it has to do some thing with encoding. I have experienced this issue, while development environment is Windows based most of the time WAMP (Works fine), where as production environment is linux based (Issue occurs). People usually suggest using file encoding as "UTF-8 without BOM" Notepad++ have this option. Some time encoding is changed by FTP client, also check that settings.

I have noticed that WAMP instance has following configuration in php.ini file which ignores the header already send error by buffering the output.

output_buffering = On

if you change it to off on your local environment you might see the header already sent error.

output_buffering = Off
PHP Avenger
  • 1,744
  • 6
  • 37
  • 66
  • Yes exactly that was the problem. It Works on WAMP but not on a linux server online because by default the output buffering was turned on in my php.ini file on local computer. thanks a lot for your explanation – dev_marshell08 Mar 24 '14 at 21:40
  • Welcome mate I am glad you found your solution. – PHP Avenger Mar 25 '14 at 08:11