Jump to content

PHP and JSON


hkalan2007

Recommended Posts

Hello,

 

I can't get a json call to work in my php... Hope some one can see where I am going wrong and push me in the right direction.

 

I have 3 values I am trying to send from the php file to the html.

 

The output looks as if it should work, but is does not... So here is the php variable that does give me a result, but I have no idea why it does not show in the div.

 

<?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);
?> 

 

Now there are no errors, and this is what the output is so I would assume it should work as the right values are there and the right id.....

 

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

 

I do not understand. Now the Jquery should be working as well... the ajax should be sending the post with json to carry the key data to make a clear result from the php and json should be doing it's job in the php file and the Jquery is do basic to have any issues, so that leaves my focus on the PHP at this time, but I do not see a proplem.

 

$(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']);          
           } 
        })           
    }});
});

 

The one thing that is odd. When I use 3 different php files, it works perfect, but that is not using json. Now that I am using json to return 1 call from the php, I am lost at this point, does anyone see what I can't...

 

Thanks,

 

Alan

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.