shan_emails has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Can you please advise on how to add date picker in cgi page using javascript

Thanks in advance
Shanmugam A.

Replies are listed 'Best First'.
Re: OT - add date picker in cgi page
by MidLifeXis (Monsignor) on Nov 14, 2008 at 15:44 UTC

    Off Topic.

    However, I know that the Yahoo User Interface has a date picker widget, as do many of the other javascript UI libraries out there.

    --MidLifeXis

Re: add date picker in cgi page
by dsheroh (Monsignor) on Nov 14, 2008 at 17:27 UTC
    I would imagine you'd do this the same way you'd add a javascript date picker to any old HTML page. The fact that the HTML is generated by a CGI program instead of being pulled from a static file doesn't have any effect on it one way or the other.
Re: add date picker in cgi page
by Your Mother (Archbishop) on Nov 14, 2008 at 23:00 UTC

    You owe me a Coke.

    use strict; use warnings; use CGI qw( header start_html end_html h1 script link ); my $jquery = "http://code.jquery.com/jquery-latest.js"; my $datepicker = "http://dev.jquery.com/view/tags/ui/latest/ui/ui.date +picker.js"; my $css = "http://dev.jquery.com/view/tags/ui/latest/themes/flora/flor +a.datepicker.css"; print header("text/html"), start_html(-title => "Pick me! Pick me!", -head => [ script({ -type => 'text/javascript', -src => $jquery }, " "), script({ -type => 'text/javascript', -src => $datepicker }, " "), link({-rel => "stylesheet", # Update! See th +e info in follow-ups -href => $css, -type => "text/css", -media => "screen", -title => "Flora (Default)"}), ], ), h1("Oh, hai! I can pix datez?"); print <<'SomeWebpadje'; <a href="http://docs.jquery.com/UI/Datepicker">UI Datepicker v1.5.2 Documentation</a>. <form> <fieldset> <legend> Pick a date! </legend> <input type="text" name="specific_date" class="any_date"/> </fieldset> </form> <script type="text/javascript"><!--//--><![CDATA[//><!-- $(document).ready(function(){ $("input.any_date").datepicker(); }); //--><!]]> </script> SomeWebpadje print end_html();

    There are caveats. First: don't keep those URIs. They're included for convenience to test. Never bite bandwidth; get the scripts installed locally or use Google's API or whatever. Next, there are some tricky CGI calls in there. Note the scripts in the head have a body of " " -- that's necessary or you don't get _end tags. Last: templating is the way to go. I actually do CGIs like this sometimes, especially just for myself, but anything meant for real production or that might grow should have its concerns split (view, controlling code, and data/model; MVC).

    Have fun. (Update: edited a functional but really weird jQuery selector.)

      I think the Link() function needs a capital L. At least in the version I'm using, link() gives an error, while Link() works perfectly.

        Hmm. Good call out. It was a poor practice on my part to use link there since it’s a built-in. CGI has been a moving target for awhile which I actually dislike… but, active maintenance is better than bit rot even when one disagrees on code direction.

        # CGI -> 3.63 perl -MCGI=link -le 'print link({-href=>"/",-rel=>"stylesheet"})' <link href="/" rel="stylesheet" />
        # CGI -> 4.28 perl -MCGI=Link -le 'print Link({-href=>"/",-rel=>"stylesheet"})' <link href="/" rel="stylesheet" />

        Changes file says it was “various beta versions” / 2.37 when it was changed, but it wasn’t really in until some point between the two above.