0

I am following this Set multiple jQuery Datepickers' value based on one jQuery Datepicker's selected date which is already asked by me and also received answer as well.

I have following multiple dates on a page and change on first date picker will change values for other date pickers on the screen.

First date picker is one which value will changes values for other date pickers.

enter image description here

Note: Please note that I want to display Date Picker from Image and don't want to use input type as text

I want to know the best way to store this values to server. You can see my current code same as suggested in answer in above question.

$("#datepicker1, #datepicker2, #datepicker3, #datepicker4").datepicker();
$("#datepicker1").datepicker("option", "onSelect", function (dateText, inst) {
    var date1 = $.datepicker.parseDate(inst.settings.dateFormat || $.datepicker._defaults.dateFormat, dateText, inst.settings);

    var date2 = new Date(date1.getTime());
    date2.setDate(date2.getDate() + 1);
    $("#datepicker2").datepicker("setDate", date2);

    var date3 = new Date(date1.getTime());
    date3.setDate(date3.getDate() + 2);
    $("#datepicker3").datepicker("setDate", date3);

    var date4 = new Date(date1.getTime());
    date4.setDate(date4.getDate() + 3);
    $("#datepicker4").datepicker("setDate", date4);
});

But I would like to implement it through js array and then want to save data from PHP.

Can anybody guide me on this?

If there would be little code help then it will be very great help for me.

1 Answer 1

1

If you want to store your data asynchronously (without full page reload) you can simply make use of jQuerys post function:

$('#submit_btn').click(function(){
    $.post( "handler.php", { 
        dp1: $("#datepicker1").val(), 
        dp2: $("#datepicker2").val(), 
        dp3: $("#datepicker3").val(), 
        dp4: $("#datepicker4").val()
    })
        .done(function( data ) {
             alert( "Data successfully stored!");
     });
});

Suggesting that you use an HTML submit button*:

<input type="submit" id="submit_btn" value="send" />

In this example, you would have a handler.php file in the same directory. In this file, you could access the datepicker values through

$_POST['dp1'], $_POST['dp2'] etc.

If you really want to pass an array for some reason (I would pass 4 values just like this), you should transfer a JSON object and decode it in php via

json_decode()

Hope this helps!!


*regarding your markup from the post you referenced, I would suggest packing all the datepickers in a element. Although not necessary, this is syntactically correct and allows you to bind to the "submit" event instead of the click event on the button.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please show me if we don't want to put it with datepicker1, datepicker2.... I would like to have an array to post. Can you please see for that? Though, I appreciating your valuable answer :) (y)
As I mentioned, your goal would be transferring a JSON object (technically it's not an array, but this is what you aim for). Just do some search around "transferring json with ajax/jquery/javascript" and you'll find lots of guidance already on the web :)
Ok. I will sure check. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.