Avatar of tjyoung
tjyoung
 asked on

Problem with datepicker and ajax post

Hi,
I hobbled together some code with a datepicker that highlights dates from my DB and when you click on a date, it sends a post. It all works fine. The event dates are mm/dd/YY format but when they are added to my url for the post they look like:
contest_pause.php?contestdate=Thu%20Mar%2028%202013%2000:00:00%20GMT-0300%20(ADT)

Somewhere in my datepicker code it spells out the date instead of leaving it as is. Can you see how I can adjust the datepicker so it leaves the date as is? I've tried a hundred different ways and they fail.

var events = [ 
{ contestdate: new Date('03/26/2013') },{ contestdate: new Date('03/25/2013') },{ contestdate: new Date('03/31/2013') },{ contestdate: new Date('03/29/2013') },];



$( ".contestpicker" ).datepicker({ 
		autoSize: true,
		numberOfMonths: 1,
		minDate: 0,
		beforeShowDay: function(date) {
        var result = [true, '', null];
        var matching = $.grep(events, function(event) {
            return event.contestdate.valueOf() === date.valueOf();
        });
        
        if (matching.length) {
            result = [true, 'highlight', null];
        }
        return result;
    },
    onSelect: function(dateText) {
        var date,
            selectedDate = new Date(dateText),
            i = 0,
            event = null;
        
        
        while (i < events.length && !event) {
            date = events[i].contestdate;

            if (selectedDate.valueOf() === date.valueOf()) {
                event = events[i];
            }
            i++;
        }
        if (event) {
            $.post("assets/db/contest.php?contestdate=" + event.contestdate + "");
        }
    }
	});	

Open in new window

JavaScriptjQueryAJAX

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
SOLUTION
David S.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Rainer Jeschor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michel Plungjan

or populate the alt field with the format you want

http://jqueryui.com/datepicker/#alt-field
tjyoung

ASKER
@Kravimir
@RainerJ
That worked like a charm. If I can bug you guys for one last question regarding it:
When I pull the dates from the DB they are actually formatted like:
2013-03-28

Currently I'm changing them to:
03/28/2013

Ideally it would be nice to keep them that way but the datepicker fails when I try that format. I can stay with the current format and change it when I receive a post, but its a bit redundant.
Can you possibly demonstrate (if its not a hassle for you), to use the DB date format and have the datepicker still work?

*I'm not even sure if that is the only way it will work, but am hoping it can with the preferred format.
Rainer Jeschor

Just rearrange the way how paramDate is generated.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Michel Plungjan

How about

$('.contestpicker').datepicker({ dateFormat: 'mm/dd/yyyy' }).val();
tjyoung

ASKER
geez, that was obvious. Thanks very much for your help, I never would have gotten it otherwise.
Michel Plungjan

Did you even look at my suggestions?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.