[英]how do i get a date value from php into a jquery variable?
<?php
$sql = "SELECT * FROM expense WHERE userid = $userid";
$expense_date= array(); //array declared to be used below
$amount = array();// array declared
$result = mysqli_query($conn, $sql);
if(!$result){
die("query failed"); // query failed due to no connection or error in query
} else {
while( $row = mysqli_fetch_assoc($result)){ // fetches infromation
array_push($amount, $row["amount"]); //pushes my distances whic are returned from the query from the database into an array
$date_entered = ( date("d-n-y", strtotime($row["timestamp"])));
array_push($expense_date, $date_entered);//pushes date travelled
}
}
//$arr = array('distance'=>$dist_covered, 'dateTravel'=>$travel_date);
//print_r(json_encode($arr)); ------ make sure distance are being inserted into the database
echo $date_entered; ?>
<script type="text/javascript">
var unavailableDates = <?php print_r(json_encode($date_entered))?>;
function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
}
else {
return [false, "", "Unavailable"];
}
}
$(function() {
$( "#Datepicker1" ).datepicker({
numberOfMonths:2,
beforeShowDay: unavailable
});
});
</script>
I am trying to get the unavailableDates
variable to pick out a date from the database. 我正在尝试获取unavailableDates
变量以从数据库中选择一个日期。 the variable which is in php is echoing out the date in the same format. php中的变量以相同格式回显日期。 but the variable is not recognizing it. 但变量无法识别它。 If i enter the dates manually it works. 如果我手动输入日期,它将起作用。
var unavailableDates = <?php echo json_encode($date_entered)); ?> || '';
Both print_r() and var_dump() are used to display data for debugging purposes. print_r()和var_dump()都用于显示数据以进行调试。 print and echo are used for outputting strings. print和echo用于输出字符串。 Using echo is most common. 使用回声是最常见的。
$d = date("d-n-y");
print_r($d);
var_dump($d);
echo $d;
Will produce: 将产生:
11-2-16
string(7) "11-2-16"
11-2-16
So, even though you're using print_r() instead of echo, the result is the same output. 因此,即使您使用的是print_r()而不是echo,结果也是相同的输出。 This is because it's a string variable in this case. 这是因为在这种情况下,它是一个字符串变量。 Or is this an array of dates? 还是这是一系列日期?
You may have another issue in your code. 您的代码中可能还有另一个问题。 Are you getting any console errors? 您是否收到任何控制台错误?
I may be missing something. 我可能会缺少一些东西。 Is unavailableDates supposed to be an array of dates? 是unavailableDates应该是日期数组吗? In that case, you might have your variables mixed up a bit. 在这种情况下,您可能会将变量混在一起。 See the array_push() PHP function. 请参见array_push()PHP函数。
See http://php.net/manual/en/function.array-push.php 参见http://php.net/manual/en/function.array-push.php
array_push($expenseDates, $date_entered);//pushes date traveled array_push($ expenseDates,$ date_entered); //推送日期
Then... 然后...
var unavailableDates = <?php echo (!empty($expenseDates)) ? json_encode($expenseDates) || []; ?>;
Right, your code here is a bit of a mess to be honest, lots of obvious issues which I have corrected below: 是的,说实话,这里的代码有些混乱,我已经在下面纠正了许多明显的问题:
<?php
$sql = "SELECT * FROM expense WHERE userid = $userid";
$expense_date = array(); //array declared to be used below
$amount = array(); // array declared
$result = mysqli_query($conn, $sql);
if (!$result) {
die("query failed"); // query failed due to no connection or error in query
} else {
while ($row = mysqli_fetch_assoc($result)) {
$amount[] = $row["amount"];
$date_entered = date("d-m-y", $row["timestamp"]); #CORRECTED - ONLY FEED IN THE TIMESTAMP
$expense_date[] = $date_entered;
}
}
//echo $date_entered; #WHY IS THIS BEING ECHOED OUT HERE FROM PHP?
?>
<script type="text/javascript">
var unavailableDates = '<?= $date_entered ?>';
function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
}
else {
return [false, "", "Unavailable"];
}
}
$(function () {
$("#Datepicker1").datepicker({
numberOfMonths: 2,
//beforeShowDay: unavailable # WHAT IS THIS?
beforeShowDay: unavailable('MISSING_DATE')
});
});
</script>
.php
file? 这一切都发生在1个.php
文件中吗? unavailable()
.js function? 看一下datepicker代码-看起来像是对unavailable()
js函数的调用? What date is that meant to call? 那叫什么日期?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.