0

I have a JQuery Datepicker modified to select week range based on day selected by user, and to submit an HTML form named "weekDate" onSelect:

$(document).ready(function()
{  
    var startDate;
    var endDate;

    var selectCurrentWeek = function() {
        window.setTimeout(function () {
            $('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active')
        }, 1);
    }

    $('.week-picker').datepicker( {
        showOtherMonths: true,
        selectOtherMonths: true,
        onSelect: function(dateText, inst) { 
            var date = $(this).datepicker('getDate');
            startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
            endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
            var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
            $('#startDate').text($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
            $('#endDate').text($.datepicker.formatDate( dateFormat, endDate, inst.settings ));

            selectCurrentWeek();

        },
        beforeShowDay: function(date) {
            var cssClass = '';
            if(date >= startDate && date <= endDate)
                cssClass = 'ui-datepicker-current-day';
            return [true, cssClass];
        },
        onChangeMonthYear: function(year, month, inst) {
            selectCurrentWeek();
        },

        onSelect : function(){
        $('#weekDate').submit();   
    }
    });

    $('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
    $('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
});

Then I have an HTML form named "weekDate" to catch the startDate and endDate values when the onSelect Datepicker function is triggered when the user selects a date:

<form id="weekDate" name="weekDate" action="~" method="post"> 
    <input type="hidden" id="startDate" name="startDate" class="week-picker" />
    <input type="hidden" id="endDate" name="endDate" class="week-picker" />
</form>

Then I have a PHP page hopefully catching the values of #startDate and #endDate as POST variables:

$UpWeekStart = $_POST['startDate'];
$UpWeekEnd = $_POST['endDate'];

My issue: When I try to echo $UpWeekStart and $UpWeekEnd, blank is outputted. Please help.

5
  • 3
    "The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method." -- api.jquery.com/text/
    – showdev
    Commented Aug 12, 2015 at 21:49
  • so like: "$('#startDate').val($.datepicker.formatDate( dateFormat, startDate, inst.settings )); $('#endDate').val($.datepicker.formatDate( dateFormat, endDate, inst.settings ));"?
    – bob
    Commented Aug 12, 2015 at 21:54
  • 1
    It looks like you're defining the onSelect callback twice, can you do that? Commented Aug 12, 2015 at 22:00
  • That was the solution! I added my "$('#weekDate').submit();" to the initial onSelect and it works now. Thank you for your help!
    – bob
    Commented Aug 12, 2015 at 22:05
  • You're welcome, good luck on the project. Commented Aug 12, 2015 at 22:31

1 Answer 1

0

You need to change .text() for .val()

$('#startDate').val();

Not the answer you're looking for? Browse other questions tagged or ask your own question.