I'm trying to get a variable to jQuery from mySql database.
Here's the php code, getTime.php:
include('connect.php');
$sql = "select * from timer";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
$hours = $row['hours'];
$minutes = $row['minutes'];
//echo $hour.":".$minutes;
}
$timeHour = $_POST['hours'];
echo $timeHours;
Here's the ajax POST request for another file:
var timeHour = "";
$.ajax({
type: "POST",
url: "ajax/getTime.php",
data: timeHour
});
$(document).ready(function() {
alert(timeHour);
});
But the alertBox doesn't show anything. It should be really easy, but i haven't found an example that it explains it.
UPDATE
Thanks! But now i have another problem - I want to use my variables "timerHours" and "timerMinutes"...
$.ajax({
type: "POST",
url: "getTime.php",
success: function(data){
//alert(data);
timerArr = data.split(":")
timerHours=timerArr[0];
timerMinutes=timerArr[1];
}
});
... but it gives 'undefined:undefined' - when i alert data - it works fine.
var today = new Date();
var endTime= new Date(today.getFullYear(), today.getMonth(), today.getDate(), timerHours, timerMinutes , 00);
console.log(timerHours+":"+timerMinutes);//Console: undefined:undefined
var second = (endTime.getMinutes()-today.getMinutes())*60+(endTime.getSeconds()-today.getSeconds());
And if I put it on the doTime function and print it to the html it's working fine too.
function do_time() {
//console.log(timerHours+":"+timerMinutes);//Console: 21:50
second--;
$("#timer").html("Time left: "+second);//Output: Time left: NaN
$('#setTime').html("Hours: "+timerHours+" Minutes: "+timerMinutes);//Works fine too.
}