Datepicker to show different value in dropdown when a specific day is chosen

Gosi

I have a datepicker and a dropdown menu with different delivery times. Basically, when the user clicks on any day from Mon to Fri, the dropdown timing menu has 4 values. But when the user clicks on Sat, the dropdown timing menu will then have 3 values.

The following is my code. (Take note I have already made changes such as disabling Sundays and dates that has past)

<script>
$( function() {
    $("#datepicker").datepicker({
        minDate: new Date(),
        beforeShowDay: function(date) {
            var day = date.getDay();
            return [(day != 0), ''];
        }
    });
    } );
</script>

<form name="checkout">
<input type="text" name="datepicker" id="datepicker" placeholder="Date of Delivery" onBlur="getWeekday();">

<div id="weekday">
    <select name="time">
        <option value="slot1">9am - 11am</option>
        <option value="slot2">11am - 2pm</option>
        <option value="slot3">2pm - 5pm</option>
        <option value="slot4">7pm - 10pm</option>
    </select>
</div>

<script>
function getWeekday() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("weekday").innerHTML = xhttp.responseText;

    }
  };
  xhttp.open("GET", "_weekday.php", true);
  xhttp.send();
}
</script>


</form>

So as you can see, in my code, I have 3 things in it which is the datepicker, the form and the ajax code.

This is just what is in the _weekday.php

<select name="time">
    <option value="slot1">9am - 11am</option>
    <option value="slot2">11am - 2pm</option>
    <option value="slot3">2pm - 5pm</option>
</select>

So basically, now what this does is that by default, the dropdown menu has 4 timeslots in it, but when the user clicks on the datepicker, it will have 3 timeslots to it.

So my question or rather the help I need is, how do I make it in such a way, that when only when any saturday is clicked on the datepicker, then the ajax activates and displays the 3 timeslots instead of the default 4. However, if its any other day, then it remains as the 4 timeslots.

Thank you.

aditya
<script>
function getWeekday() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      var pickedDate = new Date($('#datepicker').val());
   if(pickedDate.getDay() == 6){
//Write code for 3 option in select
   }
    else{
//Write code for 4 option in select
}

    }
  };
  xhttp.open("GET", "_weekday.php", true);
  xhttp.send();
}
</script>

You just neee to chack using new Date().getDay() that which day is in datetimepicker, if the day is 6 which is saturday then append only 3 option otherwise for all weekday all other 4 option

or the best way to do is to pass the day of week in request to your php page where you can write the respective logic of day in php also pass the date in query param and check every condition in php which willbe good for security point of view also

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to show to chosen day as text after the DatePicker

Show value of option when chosen on select in Angular

Show different pages or UI when a value is selected from dropdown?

Knockout observable is not updating when Chosen dropdown value changes

Dropdown will not load PDF when chosen

Make submit button active when value on chosen dropdown option is more than 0

JQuery "Chosen" dropdown cut when appearing

DropDown menu that when chosen turns option into a button

ReactJs - show another field on selection of specific value in dropdown

Laravel 8 how to show & hide or grayed out Div when another Div Dropdown selection value of foreignkey from different table

Datepicker disable before specific day

show form-control only when value chosen on previous form-control with html and php

Show the default value chosen for XX:ParallelGCThreads

how to show datetimepicker and set the chosen value in textfield

Show MessageBox when item was chosen from combobox

Reset input$mytime_selected value back to NULL in timevis when a different filter is chosen

Make a specific query based on the chosen radio value

Show sub Areas dropdown based on chosen city in WooCommerce checkout

Get value of specific DropDown when it's value is selected

Assign a value based on time and specific day (2 different variables)

Datepicker “fengyuanchen/datepicker” outputting only 'Day' into value

Fill specific text from other list according to chosen dropdown entry

Show Selected Value in Dropdown

How can you show different dropdowns for different checkboxes & then different dropdown when multiple checkboxes are checked?

SwiftUI: unfocus TextField when DatePicker date has been chosen

Show in datepicker javascript the months indicated by dropdown menu

Postgresql show specific value first, order by different criteria

Position a dropdown to a specific value

Show current day in the title of jquery datepicker