0

In the Kendo UI - Tutorial: Intro to Building Apps with Kendo UI And PHP - Part 1, I got as far as wiring up the grid.
The problem is that when the PHP file gets read, it’s getting read as HTML and not as JSON. The fix was to put the header. The suggested fix was to specify in the PHP file right before echo back that the type is JSON. by adding a header as shown below:

// add the header line to specify that the content type is JSON
header("Content-type: application/json");

echo "{\"data\":" .json_encode($arr). "}";

I followed the suggestion strictly but I get the following error message:

Warning: Cannot modify header information - headers already sent by (output started at /home/saulo/public_html/aw001/index.php:6) in /home/saulo/public_html/aw001/index.php on line 16

{"data":[{"first_name":"Richard","last_name":"Gere"},{"first_name":"George","last_name":"Clooney"},{"first_name":"Glenda","last_name":"Jackson"},{"first_name":"Lawrence","last_name":"Smith"}, etc...

HELP please!

I searched stackoverflow but I couldn't find the same problem.

Community
  • 1
  • 1
Lito
  • 1
  • It seems that you have a previous `echo` in your php file before the instruction `header`. – Samuel Caillerie Apr 23 '13 at 08:37
  • If you have a problem with a tutorial, please contact the author of that tutorial for your support options. We can not deal well here with third party code. – hakre Apr 26 '13 at 08:06

3 Answers3

1

You have already output something on on line 16 of index.php, before calling header function. You cannot do that. The header function must be used before any output is made.

Check the line 16. It can easily be only empty line. You need to remove that.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

this is because before setting the header info you are printing something, as the error says:

 headers already sent by 
Bernat
  • 1,537
  • 3
  • 18
  • 40
-1

Thanks. I got it fixed. It is now converting type to application/json. No errors showing in my Chrome console. But no data is still showing. All that shows are the headers.

index.php below:

<!DOCTYPE html>
<!-- aw001 wire up grid --> 
<html> 
    <head> 

        <link href="css/kendo.common.min.css" rel="stylesheet">
        <link href="css/kendo.metro.min.css"rel="stylesheet">
        <script src="js/jquery.min.js"></script>
        <script src="js/kendo.all.min.js"></script>
    </head>
    <body>
        <div id="grid"></div>
        <script>
            $(function() {
                $("#grid").kendoGrid({
                    dataSource: {
                        transport: {
                            read: 
                                "data/archers.php"                                
                        },
                        schema: {
                            data: "data"
                        }
                    },
                    columns: [{ field: "FirstName" }, { field: "LastName" }]
                });
            });
        </script> 
    </body> 
</html>

archers.php below:

<?php

     $link = mysql_pconnect("localhost", "saulo_admin", "admin") or die("Unable To Connect To Database Server");
     mysql_select_db("saulo_asn") or die("Unable To Connect To ASN");

     $arr = array();
     $rs = mysql_query("SELECT first_name, last_name FROM archers");

     while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
     }
     header("Content-type: application/json");
     echo "{\"data\":" .json_encode($arr). "}";

?>
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Apr 26 '13 at 08:05
  • Thanks. I didn't know. I based it on an example in the kendo tutorials. – user2313172 Apr 27 '13 at 13:53