如何将值从json传递给php

时间:2015-05-16 17:30:55

标签: json

1.php脚本

$(function () {
    var chart;
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: true
            }
        });

        $.getJSON('php2.php?ID' + Id, function (json) {
            $('#container').highcharts({

                chart: {
                    type: 'spline',
                    marginRight: 130,
                    marginBottom: 25
                },
                title: {
                    text: 'EMG'
                },
                credits: {
                    enabled: false
                },
                xAxis: {
                    type: 'datetime'

                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'EMG'
                    }
                },
                legend: {
                    enabled: false,
                    layout: 'vertical',
                    backgroundColor: '#FFFFFF',
                    align: 'left',
                    verticalAlign: 'TOP',
                    x: 50,
                    y: 35,
                    floating: true,
                    shadow: true
                },

                plotOptions: {
                    line: {
                        pointPadding: 0.2,
                        borderWidth: 0
                    }
                },
                series: [{
                    name: 'EMG',
                    data: json.data,
                    datataLabels: {
                        enabled: true,
                        rotation: -90,
                        color: '#FFFFFF',
                        align: 'right',
                        y: 10,
                        style: {
                            fontSize: '13px',
                            fontFamily: 'Verdana, sans-serif',
                            textShadow: '0 0 3px black',

                        }
                    }
                }]
            });
        });
    })
});

2.PHP

<?php
$Id =$_GET['ID'];
error_reporting(0);
$con = @mysql_connect("localhost","root","");
if (!$con) {
    echo "not connected";
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$SQL1 = "SELECT  DateandTime,EMG FROM sensor_table WHERE ID='".$Id."'";
$rows = array();
$rows['name'] = 'EMG';
$result1 = mysql_query($SQL1);
while($r = mysql_fetch_array($result1)){
    $rows['data'][] = [$r['DateandTime'],$r['EMG']];

}
json_encode($rows, JSON_NUMERIC_CHECK);
mysql_close($con);
?>

这是我的代码..我找不到我的错误..值不能在php 2中检索请帮帮我

1 个答案:

答案 0 :(得分:0)

ALTER是一个从Javascript使用的jQuery函数,而不是PHP。你应该这样做:

$.getJSON

在Javascript中,您使用$json = json_decode(file_get_contents("http://localhost/2.php?ID=" . $Id)); 来连接变量。

+

$.getJSON('2.php?ID=' + <?php echo $Id; ?>, function(json) { ... }); 中,你没有回应结果。你需要这样做:

2.php