0

I am using this Achieving Google Visualization chart reloads using ajax example to dynamically retrieve data from Mysql database and I get "Uncaught SyntaxError: Unexpected token <" error. This is my HTML file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("jquery", "1.6.1");
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">

    function drawVisualization(dataFromAjax) {
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'InvoiceNo');
        data.addColumn('string', 'B/L No');
        data.addColumn('date', 'Date');
        data.addColumn('string', 'Customer Name');
        data.addColumn('number', 'Amount');
        data.addRows(dataFromAjax);
        var table = new google.visualization.Table(document.getElementById('table'));

        table.draw(data);
   }

    function makeAjaxCall() {
      $.ajax({url:'test.php',
              data: {},
              success: function(responseData) {

                         var arrayForGviz = eval("(" + responseData + ")");
                         drawVisualization(responseData);
                       }
        });
    }
    </script>
  </head>
  <body>
    <input type="button" onclick="makeAjaxCall();return false;" value="Click to get data"></input>
    <div id="table"></div>
  </body>
</html>

and This is my PHP file test.php


    $con = mysql_connect("localhost","userName","password");

    if (!$con)

    {

    die('Could not connect: ' . mysql_error());

    }

    mysql_select_db("DB_NAME", $con);

    $result = mysql_query("call cargosys.rpt_salesByDate('2013/03/05','2013/03/10')");
    $output = array();
    while($row = mysql_fetch_array($result)) {
        // create a temp array to hold the data
        $temp = array();

        // add the data
        $temp[] = $row['inv_no'];
        $temp[] = ''' . $row['bl_no'] . ''';
        $temp[] = ''' . $row['inv_date'] . ''';
        $temp[] = ''' . $row['cust_name'] . ''';
        $temp[] = $row['Amount'];

        // implode the temp array into a comma-separated list and add to the output array
        $output[] = '[' . implode(', ', $temp) . ']';
    }

    // implode the output into a comma-newline separated list and echo
    echo implode(",\n", $output);
    //echo json_encode($output);
    mysql_close($con);

3
  • Where the error occur? is this in PHP or Javascript? And my guess is, It is happening at eval() function of javascript. Am I right? Commented Mar 11, 2013 at 11:37
  • Php syntax error are there $temp[] = ''' . $row['bl_no'] . '''; Commented Mar 11, 2013 at 12:23
  • Thanks Tuhin for pointing out that error Commented Mar 11, 2013 at 13:27

1 Answer 1

2

Inside the success function do:

console.log(responseData);

Then copy and paste responseData into the JSON validator at jsonlint.com

I am guessing it's a JSON syntax error, which the validator will point out to you.

Sign up to request clarification or add additional context in comments.

2 Comments

I checked it and it is a valid json and now it gives this error "Uncaught Error: Argument given to addRows must be either a number or an array"
its working now I changed var arrayForGviz = eval("[" + responseData + "]"); thanks for your idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.