1

I am trying to pass 2 php arrays $date_array and $drop_array into javascript so their values can be checked and pushed into a pre existing javascript array.

<?php 
$date_array = array('12-08-2016', '12-09-2016', '12-10-2016', '12-12-2016', '12-13-2016', '12-14-2016', '12-15-2016', '12-16-2016', '12-17-2016');
$drop_array = array($drop1, $drop2, $drop3, $drop4, $drop5, $drop6, $drop7, $drop8, $drop9);
?>

where $drop1-$drop9 are numeric values. I am currently passing the arrays like this and pushing their values into an array:

var date_array = '<?php echo json_encode($date_array); ?>';
var drop_array = '<?php echo json_encode($drop_array); ?>';
var disabledSpecificDays = ["12-11-2016","12-18-2016","12-16-2016","12-17-2016"];
for (var x = 0; x < drop_array.length; x++){
   var j = drop_array[x];
   if (j > 75){
      disabledSpecificDays.push(date_array[x]);
   }
}

Currently doing this and adding the dates to the array breaks my calendar. When i dont have this code and just add the dates manually to the array the calendar works fine disables the dates so i think it is how im passing the php values and trying to push them into an array

2 Answers 2

3

You are creating a string with the contents of the JSON rather than an actual JSON array. Remove the single quotes around your PHP tags.

var date_array = <?php echo json_encode($date_array); ?>;
var drop_array = <?php echo json_encode($drop_array); ?>;
Sign up to request clarification or add additional context in comments.

4 Comments

when i remove the single quotes my closing tags appear to be ignored </script> </body> </html> these tags are usually colored in atom but when i remove the single quotes around php the closing tags turn grey and my calendar doesnt work.
Without seeing the rest of your code and the HTML/javascript output it's hard to say...
<input type="text" id="datepicker" name="datepicker" placeholder="Select Date" required/> that is the calendar. I'll have to add the function for my calendar in another comment.
function disableSpecificDaysAndSundays(date) { var m = date.getMonth(); var d = date.getDate(); var y = date.getFullYear(); for (var i = 0; i < disabledSpecificDays.length; i++) { if ($.inArray((m + 1) + '-' + d + '-' + y, disabledSpecificDays) != -1 || new Date() > date) { return [false]; } } var day = date.getDay(); return [(day > 0), true]; } $(document).ready(function () { $( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd", minDate: new Date(2016, 11, 8), maxDate: new Date(2016, 11, 17), beforeShowDay: disableSpecificDaysAndSundays
-1

You need to use JSON.parse function so it parses the JSON code created by your Php code. For example:

var date_array = JSON.parse('');

The JSON.parse will convert the JSON string into a Javascript array

1 Comment

This is incorrect. You could JSON.parse() the string (wrapped in single quotes) but that is an unnecessary step if you omit the quotes and have it be a JSON object instead of a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.