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
SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
or populate the alt field with the format you want

http://jqueryui.com/datepicker/#alt-field
Avatar of tjyoung
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.
Just rearrange the way how paramDate is generated.
How about

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

ASKER

geez, that was obvious. Thanks very much for your help, I never would have gotten it otherwise.
Did you even look at my suggestions?
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo