1

索引.html:

Function drawChart() {
            var jsonData = $.ajax({
                url: "server.php",
                dataType: "json",
                async: false
            }).responseText;

            var obj = jQuery.parseJSON(jsonData);

            var data = google.visualization.arrayToDataTable(obj);

            var options = {
                title: 'Number of visitors / <?php echo $unit; ?>'
            };

            var chart = new google.visualization.BarChart(
                        document.getElementById('chart_div'));
            chart.draw(data, options);
    }       
       }

服务器.php:

$SQLString = "SELECT (...)'".$_POST['value']."' (...)
$result = mysql_query($SQLString);  
(...)
$data[$i] = array(...)
echo json_encode($data);

那么,index.html 从 server.php 获取数据对吗?我可以向 server.php 发送一些值,这些值对于在 index.html 执行 jsonData...等之前执行查询很重要吗?如何?

谢谢 :)

4

3 回答 3

1

查询参数示例:

var jsonData = $.ajax({
                url: "server.php?someQuery=" + query,
                dataType: "json",
                async: false
            }).responseText;
于 2012-11-21T13:48:53.507 回答
0

您可以通过 ajax 使用 post 方法发送。这是一个 JQuery 示例:

    $.ajax({
             type: "POST",
             url: "some.php",
             data: { name: "John", location: "Boston" }
          }).done(function( msg ) {
                    alert( "Data Saved: " + msg );
                  });

http://api.jquery.com/jQuery.ajax/

于 2012-11-21T13:48:55.020 回答
0

您可以使用 phery http://phery-php-ajax.net为您自动执行此操作的库,在您的情况下,它将是:

该事件phery:json将处理从服务器发送的 JSON

var remote = phery.remote('data', {'argument': 'you want to send'}, {'target':'server.php'}, false);
remote.bind('phery:json', function(event, obj){
  var data = google.visualization.arrayToDataTable(obj);

  var options = {
    title: 'Number of visitors / <?php echo $unit; ?>'
  };

  var chart = new google.visualization.BarChart(
    document.getElementById('chart_div'));

  chart.draw(data, options);
});

remote.phery('remote'); // call the remote AJAX function

在你的server.php

function data($data){
  $r = new PheryResponse;
  // $data['argument'] will have 'you want to send'
  $SQLString = "SELECT (...)'".$_POST['value']."' (...)"
  $result = mysql_query($SQLString);  
  (...);
  $data[$i] = array(...);
  return $r->json($data); 
}

Phery::instance()->set(array(
  'data' => 'data'
))->process();
于 2012-11-25T09:37:25.723 回答