I seem to be going no place fast...
Can't seem to get a single inline(embeded) jquery Datepicker dateText to post to the PHP and mysql correctly to return data from the table.. My reading has me to understand it should be right, but it is not. Now the datepicker select does have several events that trigger from the single select... data being requsted by the day, month, and year as well as sending the date to create a highcharts graph of data for hours of a day, days of a month, and months of a year.
The Table is very simple... date, time, power
Here is the Javasctipt...
$(document).ready(function () {
$('#datepicker').datepicker({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));
$.post('dayPower.php', {choice: dateText.val}, function(data) {
$('#apDiv2').html(data).show();
});
$.post('dayGraph.php', {choice: dateText.val}, function(data) {
$('#apDiv4').html().show();
});
$.post('monthPower.php', {choice: dateText.val}, function(data) {
$('#apDiv6').html(data).show();
});
$.post('monthGraph', {choice: dateText.val}, function(data) {
$('#apDiv9').html().show();
});
$.post('yearPower.php', {choice: dateText.val}, function(data) {
$('#apDiv8').html(data).show();
});
$.post('yearGraph', {choice: dateText.val}, function(data) {
$('#apDiv10').html().show();
});
}});
});
Here is the PHP for the POST for getting the data for the day (dayPower.php)...
<?
if(isset($_POST['choice']))
$choice = (isset($_POST['choice'])) ? date("Y-m-d",strtotime($_POST['choice'])) : date("Y-m-d");
$con = mysql_connect("localhost","root","xxxxxxxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("inverters", $con);
$sql = 'SELECT sum(power) AS power '
.'FROM feed '
.'WHERE date = $choice';
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
echo $row['power'];
?>
The first and second line of the PHP should get the date from the datepicker, and then put it into a date mysql can read, and the sql select should get the values from the table and echo it into the DIV where I have told it to go... What am I doing wrong ?
Thanks,
ALan
I now have the Jquery to post to the PHP file with this script...
$('#datepicker').datepicker({dateFormat: 'yy-mm-dd', onSelect: function(dateText) {
var myDate = $(this).datepicker('getDate');
$('#apDiv1').html($.datepicker.formatDate('DD, d', myDate));
$('#apDiv5').html($.datepicke开发者_StackOverflow中文版r.formatDate('MM', myDate));
$('#apDiv7').html($.datepicker.formatDate('yy', myDate));
$('#apDiv2').load('dayPower.php', {choice: dateText}, function() {
$(this).show();
});
$('#apDiv4').load('dayGraph.php', {choice: dateText}, function() {
$(this).show();
});
$('#apDiv6').load('monthPower.php', {choice: dateText}, function() {
$(this).show();
});
$('#apDiv9').load('monthGraph', {choice: dateText}, function() {
$(this).show();
});
$('#apDiv8').load('yearPower.php', {choice: dateText}, function() {
$(this).show();
});
$('#apDiv10').load('yearGraph', {choice: dateText}, function() {
$(this).show();
});
}});
});
As the new structure is using LOAD and not POST, my understanding is that I must use GET in the PHP, so I have made the changes that I understand should work, but they do not... I seem to be back in the same boat as before... here is the new PHP... Any ideas to why it does not work ????
<?
$choice = (isset($_GET['choice'])) ? date("Y-m-d",strtotime($_GET['choice'])) : date("Y-m-d");
$con = mysql_connect("localhost","root","xxxxxxxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("inverters", $con);
$sql = 'SELECT sum(power) AS choice '
.'FROM feed '
.'WHERE date = $choice';
$res = mysql_query($sql) or die('sql='.$sql."\n".mysql_error());
$row = mysql_fetch_assoc($res);
echo $row['choice'], '<br />';
mysql_close($con);
?>
I noticed that you're using dateText.val
. It seems to me that according to the documentation, dateText
is just a string:
The function receives the selected date as text and the datepicker instance as parameters.
Now, the second parameter to the function is the input element. This is probably what you want to call .val()
on (note the brackets as well).
Try changing:
$('#datepicker').datepicker({onSelect: function(dateText) {
To: $('#datepicker').datepicker({onSelect: function(dateText,inst) {
And then change any dateText.val
to inst.val()
.
How is the date picker formatting the date selected? All of my experience with JS date pickers returns human-readable dates, not the UNIX timestamps that I would assume you are using in your database. In that case you can use $choice_timestamped = strtotime($choice);
on the date string from the date picker then the date()
function when outputting it to display in whatever format you like.
精彩评论