jQuery Datepicker to Trigger a POST
-
06-07-2019 - |
Question
i want to use jquery UI's datepicker to trigger a POST that would then load information from the date picked on the datepicker from my database.
so i guess this is a two-part question
is there a way i can make it so the datepicker date, when selected, just gets passed into a POST, so someone would click on jan 1 2010 and it would automatically go to mysite.com?date=01012010 or something like that the way it is now the datepicker just updates a text box with the date, so upon clicking on jan 1 2010, the text box is populated with 01-01-2010
which brings me to part 2 if there is no way to do what i asked in part 1, is there a method that triggers an event on the text box being updated, that way i could just do
onTextUpdate{
redirect to - mysite.com?date=$whateverIsInTextBox
}
or something like that
please let me know if you have any solutions or ideas to do this, thanks a lot
Solution
You could do something like this if you don't want to use ajax:
$("#datepicker").datepicker({
onSelect: function(date, instance) {
window.location = "www.example.com/?date="+date;
}
});
And if you do:
$("#datepicker").datepicker({
onSelect: function(date, instance) {
$.ajax
({
type: "GET",
url: "www.example.com",
data: "date="+date,
success: function(result)
{
//do something
}
});
}
});
OTHER TIPS
use the OnSelect event of the date picker
$('id').datepicker({
onSelect: function(dateText, ins) { ... }
});
you can try other events of the datepicker control based on your requirement
There are a number of events attached to the jQuery datepicker widget; onSelect should cover point #1 above:
$('.selector').datepicker({
onSelect: function(dateText, inst) { ... }
});
In your case, you can use the $.GET or $.POST functions to pass the data to the server.