开发者

PHP, JSON, and JQUERY

I have done my reading, found no errors, but yet the php, json, and jquery are playing nice... can anyone see some thing I am not seeing here in this code for it not to work ?

My clickdates.php file has 3 values that json shpould make in 1 call... but it don't... Here is the php file (and when I test it, I do get 3 values and the 3 id's that should connect in the jquery).. Can you see something wrong that I don't ??

    <?php  
    $choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : 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);
    ?>

Here is what my test output is from the php file... in my eyes, it should work... but it don't.

{"dayPowerP":["12.7240"],"monthPowerP":["145.0460"],"yearPowerP":["941.0370"]}

Here is the very simple jquery that should be puting the values where they belong from the json... but no such luck...

    $(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']); 
            $('#apDiv6').html(json_data['monthPowerP']);     
            $('#apDiv8').html(json_data['yearPowerP']); 

           } 
        })           
    }});
});

What am I not seeing here ??

Alan


$.ajax is expecting valid json output witch the php file is not sending becouse you're not sending the right headers use

  ini_set('display_errors', 0);
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  header("Cache-Control: no-cache, must-revalidate" );
  header("Pragma: no-cache" );
  header("Content-type: application/json");
  echo json_encode($outarr);

p.s. well ofcourse you need to change the dates

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜