douguluan5102
2017-07-19 02:59
浏览 153
已采纳

需要修复Jquery错误:无效字符串JSON错误

Trying to generate a piechart using dropdown menu and api but there is a json error for table showing invalid string.

Pie file

<?php

$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'techyari_demos';

// Create connection and select db
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
?>
<html>
<head>
  <!--Load the AJAX API-->
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
  <script type="text/javascript">

  // Load the Visualization API and the piechart,table package.
  google.load('visualization', '1', {'packages':['corechart','table']});


  function drawItems(num) {
    var jsonPieChartData = $.ajax({
      url: "getpiechartdata.php",
      data: "q="+num,
      dataType:"json",
      async: false
    }).responseText;

    var jsonTableData = $.ajax({
      url: "gettabledata.php",
      data: "q="+num,
      dataType:"json",
      async: false
    }).responseText;

    // Create our data table out of JSON data loaded from server.
    var piechartdata = new google.visualization.DataTable(jsonPieChartData);
    var tabledata = new google.visualization.DataTable(jsonTableData);

    // Instantiate and draw our pie chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(piechartdata, {
      width: 700,
      height: 500,
      chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" }
    });

    // Instantiate and draw our table, passing in some options.
    var table = new google.visualization.Table(document.getElementById('table_div'));
    table.draw(tabledata, {showRowNumber: true, alternatingRowStyle: true});
  }

  </script>
</head>
<body>
  <form>
  <select name="pt" onchange="drawItems(this.value)">
  <option value="">Select a server:</option>
  <?php
   $dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'techyari_demos';
    // Make a MySQL Connection
    $con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName) or die(mysql_error());
    mysqli_select_db($con,"techyari_demos") or die(mysqli_error());
    // Create a Query
    $sql_query = "SELECT id, servername FROM server ORDER BY servername ASC";
    // Execute query
    $result = mysqli_query($con,$sql_query) or die(mysqli_error());
    while ($row = mysqli_fetch_array($result)){
    echo '<option value='. $row['id'] . '>'. $row['servername'] . '</option>';
    }
    mysqli_close($con);
  ?>
  </select>
  </form>
  <div id="chart_div"></div>
  <div id="table_div"></div>
</body>
</html>

getpiechartdata

<?php
$q=$_GET["q"];
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'techyari_demos';

$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

 $sql_query="SELECT * from entry";
 // $sql_query = "SELECT nickname, name, j2.label, j2.pointsum FROM user JOIN ( SELECT j1.user_id, j1.label, name, hover, j1.pointsum FROM activityfield JOIN ( SELECT user_id, activity_id, label, field_id , SUM( points.points ) AS PointSum FROM points JOIN activity ON points.activity_id = activity.id WHERE points.user_id=" . $q . " GROUP BY points.user_id, points.activity_id, activity.label, activity.field_id ORDER BY points.activity_id ASC ) AS j1 ON activityfield.id = j1.field_id ) AS j2 ON j2.user_id = user.id WHERE pointsum > 0 ORDER BY j2.pointsum DESC;";

  $con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName)or die(mysql_error());;
  if (!$con){ die('Could not connect: ' .mysqli_error());}
  mysqli_select_db($con,"techyari_demos");
  $result = mysqli_query($con,$sql_query);
  echo "{ \"cols\": [ {\"id\":\"\",\"label\":\"Name-Label\",\"pattern\":\"\",\"type\":\"string\"}, {\"id\":\"\",\"label\":\"PointSum\",\"pattern\":\"\",\"type\":\"number\"} ], \"rows\": [ ";
  $total_rows = mysqli_num_rows($result)or die(mysqli_error());;
  $row_num = 0;
  while($row = mysqli_fetch_array($result)){
    $row_num++;
    if ($row_num == $total_rows){
      echo "{\"c\":[{\"v\":\"" . $row['date'] . "-" . $row['sname'] . "\",\"f\":null},{\"v\":" . $row['status'] . ",\"f\":null}]}";
    } else {
      echo "{\"c\":[{\"v\":\"" . $row['date'] . "-" . $row['sname'] . "\",\"f\":null},{\"v\":" . $row['status'] . ",\"f\":null}]}, ";
    }
  }
  echo " ] }";
  mysqli_close($con);
?>

gettabledata

<?php
  $q=$_GET["q"];
 $dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'techyari_demos';


    $sql_query="SELECT date,sname,dbs,status,updatedby from entry";

  $con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
  if (!$con){ die('Could not connect: ' . mysql_error()); }
  mysqli_select_db($con,"techyari_demos");
  $result = mysqli_query($con,$sql_query);

  echo "{\"c\":[{\"v\":\"" .'date'. "\",\"f\":null},{\"v\":\"" .'sname' . "\",\"f\":null},{\"v\":\"" .'dbs' . "\",\"f\":null},{\"v\":\"" .'status'. "\",\"f\":null},{\"v\":\"".'updatedby'."\",\"f\":null}]}, ";

  $total_rows = mysqli_num_rows($result);
  while($row = mysqli_fetch_array($result)){
    echo "{\"c\":[{\"v\":\"" . $row['date'] . "\",\"f\":null},{\"v\":\"" . $row['sname'] . "\",\"f\":null},{\"v\":\"" . $row['dbs'] . "\",\"f\":null},{\"v\":\"" .$row['status']."\",\"f\":null},{\"v\":\"" . $row['updatedby']. "\",\"f\":null}]}, ";
  }

 /* $result = mysqli_query($sql_query2);
  while($row = mysqli_fetch_array($result)){
    echo "{\"c\":[{\"v\":\"" . $row['servername'] . "\",\"f\":null},{\"v\":\"" . "\",\"f\":null},{\"v\":\"" . "\",\"f\":null},{\"v\":\" Total \",\"f\":null},{\"v\":\"" . $row['dbs'] . "\",\"f\":null}]}";
  }
  echo " ] }";*/
  mysqli_close($con);
?>

1条回答 默认 最新

相关推荐 更多相似问题