I have a PHP file using json_encode to make a single call, but it does not go to the javascript... I have been trying for over a week... Maybe it is right in front of my face and I don't see it, so I will post it here to have a second set of eyes to maybe see what I don't... Does anyone see a problem, as I really dont...
Here is the json_encode getting the 3 array to make 1 call to the javascript...
$outarr['dayPowerP'] = $dayPowerP;
$outarr['monthPowerP'] = $monthPowerP;
$outarr['yearPowerP'] = $yearPowerP;
echo json_encode($outarr);
Here is the output of the php file... The ID's and the values from the sql are right there....
{"dayPowerP":["13.2470"],"monthPowerP":["193.6810"],"yearPowerP":["989.6720"]}
Here is the ajax showing the post and Json...
$(document).ready(function () {
$('#datepicker').datepicker({maxDate: 0, dateFormat: 'yy-mm-dd', onSelect: function(dateText) {
var myDate = $(this).datepicker('getDate');
$('#apDiv1').html($.datepicker.formatDate('DD, d', myDate));
$('#apDiv5').html($.datepicker.formatDate('MM', myDate));
$('#apDiv7').html($.datepicker.formatDate('yy', myDate));
$.ajax({
type: "POST",
url: "clickdates.php",
data: {choice: dateText},
dataType: "json",
success: function(json_data) {
$('#apDiv2').html(json_data['dayPowerP']).show();
$('#apDiv6').html(json_data['monthPowerP']).show();
$('#apDiv8').html(json_data['yearPowerP']).show();
}
})
}});
});
To make it really clear... here is the full PHP file.
<?php
$choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['cho开发者_运维问答ice'])) : date("Y-m-d");
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("inverters", $con);
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE date = '".$choice."' group by date"; $res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
$dayPowerP = array( $row['choice']);
?>
<?php
$choice = (isset($_POST['choice'])) ? date("m",strtotime($_POST['choice'])) : date("m");
$con = mysql_connect("localhost","root","xxxxxx");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("inverters", $con);
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE month(date) = '".$choice."'";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
$monthPowerP = array($row['choice']);
?>
<?php
$choice = (isset($_POST['choice'])) ? date("Y",strtotime($_POST['choice'])) : date("Y"); $con = mysql_connect("localhost","root","xxxxxx");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("inverters", $con);
$sql = "SELECT sum(power/1000) AS choice FROM feed WHERE year(date) = '".$choice."'";
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
$yearPowerP = array( $row['choice']);
?>
<?php
$outarr['dayPowerP'] = $dayPowerP;
$outarr['monthPowerP'] = $monthPowerP;
$outarr['yearPowerP'] = $yearPowerP;
echo json_encode($outarr);
?>
This is not complex code, so there has to be something minor I am not seeing or doing.
Thanks
Alan
Mate. Json_encode doesn't support associative arrays.
"json_data['dayPowerP']" won't work. json_data.dayPowerP should work, it looks like that will return an array, so you need to typecast that to a float.
Can you please post the result of this: (see the console)
** console.log(json_data);**
specifically
success: function(json_data) {
console.log(json_data)
$('#apDiv2').html(json_data['dayPowerP']).show();
$('#apDiv6').html(json_data['monthPowerP']).show();
$('#apDiv8').html(json_data['yearPowerP']).show();
}
If i'm right, then you should replace json_data['dayPowerP'] with json_data.dayPowerP[0] in the success function and all should work correctly.
There's no need to put each individual element in its own array; just embed them directly.
Use this: $('#apDiv2').html(json_data.dayPowerP[0]).show();
精彩评论