I have a JavaScript calender that when a user clicks on a date, the date is submitted together with other fields when the User submits the FlaskForm for validation.
I've managed to obtain the date dynamically, but when I set the date as the FlaskForm field value, the form validates but the date field comes out empty.
How can I properly submit the dynamic date data to FlaskForm using JavaScript?
Python
if createslot_form.validate_on_submit():
slot = Slot(
start_time=str(createslot_form.start_time.data),
end_time=str(createslot_form.end_time.data),
quantity=int(createslot_form.quantity.data),
date=str(createslot_form.date.data),
room_id=int(room_id)
)
db.session.add(slot)
db.session.commit()
print('slot has been added')
return redirect(url_for('edit_room', room_id=room_id))
return render_template('room.html', room=room, createslot_form=createslot_form, created_slots_json=created_slots_json, room_link=str(room_link))
Javascript
function new_event(event) {
if ($(".active-date").length != 0) {
var day = $(".active-date")[0].innerText;
var month = $(".active-month")[0].innerText;
var year = $(".year")[0].innerText;
var click_date = day + ' ' + month + ' ' + year;
console.log($(".form-control")[0]);
$(".form-control")[0].value = click_date;
$(".form-control")[0].placeholder = click_date;
}
}
Related
To achieve greater performance with our Shopify store, we decided to minimize use of jQuery across the site. In cartpage, I added a pure JavaScript datepicker that sometimes displays the calendar just once and never afterwards. I ensured only one version of jQuery loads in the theme, thinking multiple instances from other Shopify apps might be causing this issue. But even with a single jQuery instance being loaded now in the theme, the issue still persists. I don't see any console errors. Please add a product to the cart and go to the cart page to see this issue by clicking the date picker. Below is the link to the preview theme.
Following is the datepicker code for pickaday plugin.
<div class="cart-attribute__field">
<p>
<label for="ship_date">Ordering early? Pick a future ship date here:</label>
<input id="ship_date" type="text" name="attributes[ship_date]" value="{{ cart.attributes.ship_date }}" />
</p>
</div>
<p class="shipping-text">If you are placing an order with <b>a future ship date</b> that <u>also</u> includes multiple addresses please email support#packedwithpurpose.gifts upon placing your order to confirm your preferred ship date.</p>
<!-- Added 11162020 -->
<p class="shipping-text">Please note that specifying a date above ensures your gift is packaged and shipped on that date. <b>This is not a delivery date.</b> As we work with third party shipping agencies, your delivery date is subject to the specific carrier selected as well as your shipping destination. Please find our estimated shipping transit times for all regions of the US <strong>here</strong>.</p>
<script type="text/javascript">
(function() {
var disabledDays = ["2022-12-23","2022-12-24","2022-12-25","2022-12-30","2022-12-31","2023-1-1"];
var minDate = new Date();
var maxDate = new Date();
maxDate.setDate((maxDate.getDate()) + 60);
minDaysToShip = 2; // Default minimum days
if (minDate.getDay() == 5) {
// Friday. Set min day to Tuesday. 4 days from now.
minDaysToShip = 4;
} else if (minDate.getDay() == 6) {
// Saturday. Set min day to Tuesday. 3 days from now.
minDaysToShip = 3;
}
minDate.setDate(minDate.getDate() + minDaysToShip);
var picker = new Pikaday(
{
field: document.getElementById('ship_date'),
format: 'MM/DD/YYYY',
disableWeekends: 'true',
toString(date, format) {
// you should do formatting based on the passed format,
// but we will just return 'D/M/YYYY' for simplicity
const day = ("0" + date.getDate()).slice(-2);
// Get two digit month ("0" + (this.getMonth() + 1)).slice(-2)
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const year = date.getFullYear();
return `${month}/${day}/${year}`;
},
parse(dateString, format) {
// dateString is the result of `toString` method
const parts = dateString.split('/');
const day = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1;
const year = parseInt(parts[2], 10);
return new Date(year, month, day);
},
firstDay: 0,
minDate: minDate,
maxDate: maxDate,
disableDayFn: function(inputDate) {
// Disable national holidays
var formattedDate = inputDate.getFullYear() + '-' + (inputDate.getMonth() + 1) + '-' + inputDate.getDate();
return ((disabledDays.indexOf(formattedDate) == -1) ? false : true);
}
});
})();
</script>
This was fixed by replacing (function() with window.addEventListener("load", function()
This time I have question how to convert date into dynamic field by "m-d-Y".
CF7 code
<label>Date of Birth</label>
[date* date-dob class:form-control placeholder id:idate "mm/dd/yyyy"]
[dynamictext xxx id:pdfdate "date-dob format='m/d/Y'"]
output result :
The result in dynamic field is not "m/d/Y" there is come by default "Y-m-d".
Thank you for any help.
This script will instantly read values typed into your fields and will update the full address dynamic field.
You also need to add id to your dynamictext [dynamictext xxx id:pdfdate]
<script>
var pdfdate = document.getElementById("pdfdate");
var fullDate = document.getElementById("idate");
fullDate.addEventListener("input", (e) => {
var dates = e.target.value.split("-");
var dateformats = ""
if (dates.length == 3) {
var dateformats = dates[1] + "/" + dates[2] + "/" + dates[0];
}
pdfdate.value = dateformats
});
</script>
I currently have a start date and end date date picker which gets pre-populated with dates on page load. If the user clicks the next button with out interacting with the date picker I cannot get the correct date thats in the picker and only the date as a string.
I can however retrieve the date from the onChange event but this could be to late in some instances.
Here is an example of my code to explain more:
startDateAccomm = flatpickr("#StartDate", {
minDate: instance.selectedDates[0],
dateFormat: dateFormat,
onChange: function (selectedDates, dateStr, instance) {
if (selectedDates.length > 0) {
endDateAccomm.config.minDate = selectedDates[0];
if (startDateAccomm.selectedDates.length > 0) {
startDateSelected = startDateAccomm.selectedDates[0].getFullYear() + "-" + (startDateAccomm.selectedDates[0].getMonth() + 1) + "-" + startDateAccomm.selectedDates[0].getDate();
}
if (endDateAccomm.selectedDates.length > 0) {
endDateSelected = endDateAccomm.selectedDates[0].getFullYear() + "-" + (endDateAccomm.selectedDates[0].getMonth() + 1) + "-" + endDateAccomm.selectedDates[0].getDate();
}
}
if () {
//do something here.
}
}
I want to set the min date to the date thats prepopulated on page load but cannot get in the correct format as instance is not defined until the date picker is interacted with.
Any help is greatly appreciated.
After trying out the various events and hooks provided by flatpickr https://flatpickr.js.org/events/#hooks I was able to use the onReady event.
onReady: function (selectedDates, dateStr, instance) {
startDateSelected = selectedDates[0].getFullYear() + "-" +
(selectedDates[0].getMonth() + 1) + "-" + selectedDates[0].getDate();
},
I am new to the javascript/aspx world and most of my programming was done through trial and error and lots of internet searching, but i've hit upon a problem i cannot find a solution for.
I have a popup page that has 2 input boxes, and i have managed to add default values to both of these input boxes, for dates, like so:
$(window).load(function () {
var now = new Date();
var month = (now.getMonth() + 1);
var day = now.getDate();
if (month < 10)
month = "0" + month;
if (day < 10)
day = "0" + day;
$('#FNewDate').val('01/' + month + '/' + now.getFullYear());
$('#TNewDate').val(day + '/' + month + '/' + now.getFullYear());
}
now, if the user has entered a new date and hits the submit button, the page posts and reloads with the calculated results displayed to the user AND THE DEFAULT VALUES again, but not with the new dates the user has entered, and i need it to stay with the entered information.
I have tried playing around with static members but i have not been able to get it to work.
Here is the button action:
<asp:Button ID = "Calculate" runat = "server"
style="width:40% ; font:15px arial" Text = "Calculate" onclick="Calculate_Click" />
any help on the above would be appreciated, and pls include code in your replies...
Thnks
You should change your default value setters to add
if ($('#FNewDate').val().length() != 0)
To only set the value in case the value coming from server is empty.
Then in your input
<input type="text" id="FNewDate" value="<%=someObject.getSomeDate()%>"/>
I am using the JavaScript-based popup calendar from dynamic drive named
"Xin's Popup Calendar" and it works perfectly. But, I want to be able to
adjust the date of a 2nd textbox based on the selection.
For example, I want to be able to automatically adjust it to +1 month of
whatever date was selected in the popup. How can I do that?
Here is an example:
<input type="text" name="firstinput" size=20>
<small>Select Date</small>
<p>
<input type="text" name="secondinput" size=20>
<small>Select Date</small>
</p>
If the firstinput date is 3/21/10, I want the secondinput to change to 4/21/10 at the same time.
There are two parts to this: (1) a function to add a month to a data and (2) the change event handler to the textbox. This solution is very specific to the Xin Pop Up Calendar.
function addMonth(d,month){
t = new Date (d);
t.setMonth(d.getMonth()+ month) ;
if (t.getDate() < d.getDate())
{
t.setDate(0);
}
return t;
}
$("input[name='firstinput']").change(function(){
var dt = $(this).val();
var yr = dt.substring(0,4);
var mo = dt.substring(5,7);
var dy = dt.substring(8,10);
var firstDate=new Date();
firstDate.setFullYear(yr,mo-1,dy);
var secondDate = addMonth(firstDate,1);
$("input[name='firstinput']").val(secondDate.getFullYear() + "/" + secondDate.getDate() + "/" + secondDate.getMonth()+1);
});