Search code examples
javascriptphpjquerysqlbootstrap-datetimepicker

JavaScript variables to PHP Form on submit


I use the following code :

// Datepicker / Timepicker jQuery Script
$('#datetimepicker1').datetimepicker({
    format : "YYYY-MM-DD"
}).on("dp.change", function (e) {
    datetimepicker1 = $('#datetimepicker1').val();
});

How do I get my PHP form to get the variable 'datetimepicker1' on form submission to send it to my SQL database.

Thanks for any help you can provide.


Solution

  • as posted in the comments - you can do it through AJAX, or you can set a hidden input value in your form so that it passes with the rest of the form on submission. The following creates a hidden input which you would put into your existing form, and then on the change in the date-time picker, will add the value to this hidden input. The form can then submit as normal and the value will be passed with the rest of the form variables.

    //input in form
    <input type='hidden' name='new_dateTime' value='' />
    
    
    // Datepicker / Timepicker jQuery Script
    $('#datetimepicker1').datetimepicker({
        format : "YYYY-MM-DD"}
        ).on("dp.change", function (e) {
            datetimepicker1 = $('#datetimepicker1').val();
    $('[name = new_dateTime]').val(datetimepicker1);
        });