简体   繁体   中英

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. the variable which is in php is echoing out the date in the same format. 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 and echo are used for outputting strings. 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. 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? In that case, you might have your variables mixed up a bit. See the array_push() PHP function.

See http://php.net/manual/en/function.array-push.php

array_push($expenseDates, $date_entered);//pushes date traveled

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>
  1. Is this all happening in 1 .php file?
  2. Look in the datepicker code - looks like that is meant to be a call to the unavailable() .js function? What date is that meant to call?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM