-2

I don't quite understand how ajax works, esp the "data" on it. I know what to put in url and type but on data I have no idea what to put there and what's is data on AJAX.

I want to fetch data on my database using AJAX, by calling php script using AJAX. I want to get dates that is fully booked and disable it on the datepicker plugin I used.

<script type="text/javascript">

       let picker = new Lightpick({
       field: document.getElementById('lightpick-datepicker'),
       minDate: new Date(),
       disableWeekends: true
   });

   let disabledDates = // here ajax request to fetch unavailable dates as array. 
   // for example fetch data has returned that array: 
   // [
   //    ['2019-02-05', '2019-02-11'],
   //    ['2019-02-14', '2019-02-16']
   // ]

   // set to picker unavailable dates
   picker.setDisableDates(disabledDates);

</script>

this is my code above and this how I think how this code should work or am i wrong? I just dont know what to put there and how does AJAX works.

If ever someone actually solve/help me on this, Can you please explain oh how calling php script ajax works? because I might use this "calling php script using AJAX" often on my project.

I tried search about this call php script using AJAX thing. But they explain poorly on how it works they only give codes though the codes they give doesn't solve my problem.

Tried this code::

<script type="text/javascript">

       let picker = new Lightpick({
       field: document.getElementById('lightpick-datepicker'),
       minDate: new Date(),
       disableWeekends: true
   });

   let disabledDates = ({
        $.ajax({

            url: '/action/get_dates.php';
            type: 'POST';
            data: {} // i dont know that to put here

        });
   )};

   // set to picker unavailable dates
   picker.setDisableDates(disabledDates);

</script>
  • 2
    There are myriad questions and answers here on Stack Overflow covering this subject. Firstly, you need to choose how you will be performing the request in JavaScript (jQuery, Axios or other libraries) and then search the site—there will definitely be a useful answer already. – Darragh Enright Feb 13 '19 at 14:35
  • In you need an introductory tutorial, then you should start by reading an introductory tutorial (e.g. [this one](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)) and not asking a SO question. – Quentin Feb 13 '19 at 14:37
  • Use jquery library quit simple and rhere are many samples available for calling php script using jquery – user3563774 Feb 13 '19 at 14:44
  • use this syntax in a html file with jquery library:$.getJSON( "ajax/test.json", function( data ) { var items = []; $.each( data, function( key, val ) { items.push( "
  • " + val + "
  • " ); }); $( "
      ", { "class": "my-new-list", html: items.join( "" ) }).appendTo( "body" ); }); – user3563774 Feb 13 '19 at 14:49
  • Probably a better duplicate for https://stackoverflow.com/questions/2276463/how-can-i-get-form-data-with-javascript-jquery – William Perron Feb 13 '19 at 15:46