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

please can you suggest example code (or links that offer scholarship) on achieving the functional requirements described via list-items "a" and "b" following: a) insertion of a calendar object within a CGI script which once clicked open will display a graphical image of a calendar which shows current year, month, and day and can be moved forward or back in time to select any desired date by clicking on the day in the calendar b) return the selected date in yyyy-mm-dd format
  • Comment on obtaining yyyy-mm-dd via a calendar object

Replies are listed 'Best First'.
Re: obtaining yyyy-mm-dd via a calendar object
by rhesa (Vicar) on Jul 21, 2006 at 01:22 UTC
    I suppose this is actually a JavaScript/DHTML question, and isn't really related to Perl in any way. But I'll give you a link to the calendar control we use, and like: http://www.dynarch.com/projects/calendar.
Re: obtaining yyyy-mm-dd via a calendar object
by davorg (Chancellor) on Jul 21, 2006 at 07:45 UTC

    As far as Perl is concerned, the answer to your question is in two parts:

    1. Work out the combination of HTML and Javascript that you need to create the control that you want
    2. Use Perl to include that combination of HTML and Javascript into the output from your CGI program

    For part one, you'll need to ask for help somewhere that specialises in HTML and Javascript. If you have trouble with the second part we'll be happy to help you.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: obtaining yyyy-mm-dd via a calendar object
by gmacfadden (Sexton) on Jul 26, 2006 at 20:32 UTC
    thank you for the previous responses. they were helpful in the areas of subsequent scholarship. specifically i learned how to accomplish the initial desired task at the bottom of this thread by placing a calendar widget (using javascript) in static HTML - as in the following example static html code and wherein everything works fine. for purposes of differentiation, i shall refer to this solution (following) as the "code-in-html" solution.
    <HTML><HEAD><script type='text/JavaScript' src='scw.js'></script></HEA +D><BODY> <FORM action="http://www.e1surveys.com/cgi-bin/testform1.cgi" method=" +POST"> Enter date:<input name='birthdate' id='date1' type='text' value=''/> <img src='calendar.jpg' title='Click Here' alt='Click Here' onclick="scwShow(document.getElementById('date1'),this);" /><br/> Enter your name: <INPUT TYPE="text" NAME="name" SIZE="30"> <BR> <INPUT TYPE="submit" VALUE="Submit" NAME="Submit Button"> <INPUT TYPE="reset" VALUE="Reset" NAME="Reset Button"> </FORM></BODY></HTML>
    now here's the rub - continuing scholarship in perl has opened the possibility of an alternative approach using the same javascript (calendar widget) but this time within an "html-in-code" solution (see the below perl code) which generates html dynamically. when the static html generated by the "code-in-html" solution, is viewed side-by-side with the dynamic html generated by the "html-in-code" solution, the two htmls generated are materially the same. however of course, while closeness works in horseshoes, it fails in perl execution.
    the line of code "print header ($html1)" causes a unexpected user prompt asking if the user wants to download the referenced scw.js javascript file (which already exists in the same directory as my cgi script server side) - - and then the program abends without giving any errors. this does not occur using the static html solution, or if the line of code is changed to "print header ()" - but of course that negates use of the calendar widget'.
    in order to grow in my understanding of perl, i would like to pursue the more flexible (ultimatedly), albeit more difficult to implement (at least for me) below "html-in-code" (dynamic html) solution. i get the sense that the root of my problem is within the dynamically generated header data, but don't know how to navigate this obstacle. your help would be welcome. thx.
    #! /usr/bin/perl -wT use CGI qw(:standard escapeHTML); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; my $html1 = qq(<script type='text/JavaScript' src='scw.js'></script>); my $html2 = qq(<input name='birthdate' id='date1' type='text' value='' +/> <img src='calendar.jpg' title='Click Here' alt='Click Here' onclick="scwShow(document.getElementById('date1'),this);" /><br/>); print header ($html1), start_html (); print p ("Element names & values submitted in previous form:"); my @names = param (); # get list of parameter names if (!@names) { print p ("(no elements present)"); } else { my @item = (); foreach my $name (@names) { my @val = param ($name); $val[0] = "[" . join (", ", @val) . "]" if @val > 1; push (@item, escapeHTML ("$name: ($val[0])\n")); } print ul (li (\@item)); } print hr (); print start_form (-action => url ()), p ("Enter date:", $html2), br(), p ("Enter your name:", textfield (-name =>"name", -type=>"text", -size +=>"30")), br(), submit (-name => "Submit Button", -value => "Submit"), reset (-name => "Reset Button", -value => "Reset"), end_form (); print end_html (); exit (0);
      Have a look at the CGI documentation, specifically the section on "CREATING THE HTML DOCUMENT HEADER".

      The CGI header() function is used to generate a HTTP header, which relates to the communication protocol. You should not pass anything to it unless you know what it's for.

      What you're looking for, then, is:

      print header(), start_html( -script => $html1 );
        Your CPAN lead was helpful -thanks. FYI, the link http://home.netscape.com/eng/mozilla/2.0/handbook/javascript/ cited within the CPAN is defunct. Can you offer a viable url for the intended link?

        Based on this, here's where i'm at. let's take something a bit more simple to work out my kinks between Perl and Javascript (using what was in CPAN).

        a) it appears the javascipt must be put in-line with the Perl code as in the below code. I'd rather be able to keep the javascript separate if there is a way to do so...the major problem I see with this approach is the javascript source is printed out in it's entirety! It just clutters up the screen. So the first question, is how does one suppress the printout of the javascript?

        b) second question, i want to reference the variable 'answer' inside the javascript and place it in the form textfield 'answer'

        #! /usr/bin/perl -wT use CGI qw(:standard escapeHTML); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header (), my $JSCRIPT=<<END; // Ask a silly question function riddle_me_this() { var r = prompt("What walks on four legs in the morning, " + "two legs in the afternoon, " + "and three legs in the evening?"); response(r); } // Get a silly answer function response(answer) { if (answer == "man") alert("Right you are!"); else alert("Wrong! Guess again."); } END print start_html(-title=>'The Riddle of the Sphinx', -script=>$JSCRIPT); print p ("Element names & values submitted in previous form:"); my @names = param (); # get list of parameter names if (!@names) { print p ("(no elements present)"); } else { my @item = (); foreach my $name (@names) { my @val = param ($name); $val[0] = "[" . join (", ", @val) . "]" if @val > 1; push (@item, escapeHTML ("$name: ($val[0])\n")); } print ul (li (\@item)); } print hr (); print start_form (-action => url ()), p ("Enter riddle answer:", textfield (-name =>"answer", -id=>'response +1' -type=>"text", -size=>"30"), button(-name=>'riddle_button', -valu +e=>'Ask A Riddle', -onClick=>"riddle_me_this(response1)")), br(), p ("Enter your name:", textfield (-name =>"name", -type=>"text", -size +=>"30")), br(), submit (-name => "Submit Button", -value => "Submit"), reset (-name => "Reset Button", -value => "Reset"), end_form (); print end_html (); exit (0);