2

I have two date pickers on my website and I'd like to know how to get the chosen date once the user clicks on "submit", and send the user to a page with the datepicker's data in it like this:

www.url.com/start=2017-01-28&end=2017-01-29

So in short, I need to get the data and put it after "=" in the url, then automatically send the user to this url.

How can I do this please ?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
François Malet
  • 99
  • 1
  • 3
  • 14
  • The datepicker library should expose events you can hook to. You need to add your code to the event which is called when a selection is made which appends the selected date value to the URL and then call `window.location.assign()`. Which datepicker library are you using? Also note that I would guess that `/` in your example URL should be a `?` – Rory McCrossan Jan 20 '17 at 09:29

4 Answers4

1

I have created one method called handleFilterEvent each time you change the date it will hit the method with start date & end date params. Then it will create a URL with start date & end date and finally it will redirect to the new URL.

             $(document).ready(function(){
                $('.daterange-btn').daterangepicker(
                    {
                        ranges   : {
                            'Today'       : [moment(), moment()],
                            'Yesterday'   : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                            'Last 7 Days' : [moment().subtract(6, 'days'), moment()],
                            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                            'This Month'  : [moment().startOf('month'), moment().endOf('month')],
                            'Last Month'  : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                        },
                        startDate: moment().subtract(29, 'days'),
                        endDate  : moment(),
                    },
                    function (start, end) {
                        $('.daterange-btn').find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'))
                        handleFilterEvent(start,end);
                    }
                )
                function handleFilterEvent(startDate = null,endDate = null){
                    var url = "https://www.google.com"+'?';
                    var startDate = startDate ? startDate.format('YYYY-MM-DD') : '';
                    var endDate = endDate ? endDate.format('YYYY-MM-DD') : '';
                    var params = {};
                    if(startDate && endDate){
                        params.start_date =  startDate;
                        params.end_date =  endDate;
                    }
                    url+= jQuery.param( params );
                    location.href = url;
                }
            });
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
0

You can try this code using datepicker onSelect event jsfiddle.net/bharatsing/Lhpnctgv/2/

<label>Start</label>
<input type="text" id="btnStart" class="jsDate" />
<br/>
<label>End</label>
<input type="text" id="btnEnd" class="jsDate" />


$(".jsDate").datepicker({
    onSelect: function(dateText, inst) {    
        var date = $(this).val();

        if($("#btnStart").val()!="" && $("#btnEnd").val()!=""){
            var url='www.url.com/start='+$("#btnStart").val()+'&end='+$("#btnEnd").val();
            alert(url);
        }
    }
});
Bharatsing Parmar
  • 2,435
  • 1
  • 9
  • 18
  • 1
    Ok thanx i'll try this but first I'm trying to customize the datepicker options to change language for exemple. Tried this but it doesnt work : $( function() { $.datepicker.regional[ "fr" ]; $( ".datepicker" ).datepicker(); } ); – François Malet Jan 20 '17 at 09:46
  • Ok seems to work well but how do I replace the dates here :var url=('www.url.com/start=2017-01-28&end=2017-01-29';) by btstart and btnend's values ? (Sorry I'm a beginner) – François Malet Jan 20 '17 at 09:49
  • You can replace it as in answer like `var url='www.url.com/start='+$("#btnStart").val()+'&end='+$("#btnEnd").val();` – Bharatsing Parmar Jan 20 '17 at 09:51
  • Oh no doesn't seem to work :/ Do I need to replace my basic "$( function() { $( ".jsdate" ).datepicker();} )" with your js code or put it after it ? EDIT : Because I replaced it and the datepicker doesn't start anymore – François Malet Jan 20 '17 at 09:53
  • You just need to add `onSelect` event in your jquery datepicker code – Bharatsing Parmar Jan 20 '17 at 09:55
  • 1
    Works great thank you! Can you help me with my "regional" option problem? Don't understand where to put the code $( function() { $.datepicker.regional[ "fr" ]; $( ".datepicker" ).datepicker(); } ); – François Malet Jan 20 '17 at 10:00
  • You can try this to localize it http://stackoverflow.com/questions/494958/how-do-i-localize-the-jquery-ui-datepicker – Bharatsing Parmar Jan 20 '17 at 10:04
0

You can use like this

https://jsfiddle.net/shantaram/qhob15fw/

$(function(){
    
    $('.thedate').datepicker({
        dateFormat: 'yy-dd-mm',//2017-01-29
    });
    
});
$('#id-submit').click( function(e){
    e.preventDefault();
    var url = "www.url.com/?";//start=2017-01-28&end=2017-01-29
    url += "start="+ $("#id-start").val() + "&end=" + $("#id-end").val();
    alert(url);
})
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>


First Date :
<input id="id-start" class="thedate" type="text" />
<br />Last Date :
<input id="id-end" class="thedate" type="text" />
<br />
<form>
  <input id="id-submit" type="button" value="Submit" />
</form>
Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42
0

So here is what I tried but clicking doesn't seem to start the alert :

 $(function(){

 $('.jsdate').datepicker({
    dateFormat: 'yy-dd-mm',//2017-01-29
    altField: '#thealtdate',

});

});
$('.button1').click( function(e){
e.preventDefault();
var url = 
"https://www.url.com/etc.";
url += "&startdate="+ $("#id-start").val() + 
"&enddate=" + $("#id- end").val();
alert(url);
})</script>
<div class="date">
<p>Arrivée:<a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>   
<input type="text" id="id-start" class="jsdate"></p>
<p></p>
<p>Départ:&nbsp;&nbsp; <input type="text" id="id-End" class="jsdate"></p>
<p>
<input class="button1" type="button" value="Submit" /></p>

Also what is "altfield : #thealtdate" ?

François Malet
  • 99
  • 1
  • 3
  • 14
  • It probably sounds silly but I'm a newbie so I have no idea what this document.ready might be :/ As I dont use much JS I put it all directly in my HTML between two – François Malet Jan 20 '17 at 11:23
  • Im googling it now and come back to you if I need further help EDIT: By the way should I put it in a document.ready ou shouldnt I ? – François Malet Jan 20 '17 at 11:25
  • Is this what I need to do : $( document ).ready(('.button1').click( function(e){ e.preventDefault(); var url = "https://www.nightsbridge.co.za/bridge/book?action=2&bbid=17553&lang=en&"; url += "startdate="+ $("#idstart").val() + "&enddate=" + $("#idend").val(); alert(url); }); ); – François Malet Jan 20 '17 at 11:30
  • Actually to answer correctly I just copy/pasted your code from the snipper to my HTML in a – François Malet Jan 20 '17 at 11:32
  • read it here: https://learn.jquery.com/using-jquery-core/document-ready/ – Shantaram Tupe Jan 20 '17 at 11:33