in reply to obtaining yyyy-mm-dd via a calendar object

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);

Replies are listed 'Best First'.
Re^2: obtaining yyyy-mm-dd via a calendar object
by rhesa (Vicar) on Jul 26, 2006 at 21:05 UTC
    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);
        I'd rather be able to keep the javascript separate if there is a way to do so...

        You can always generate it as a separate file, and link it into your HTML. Also, Perl has a number of Templating modules, such as HTML::Template and Template Toolkit, which are designed to cleanly pull the HTML (and, by extension, Javascript embedded in HTML and/or "pure" Javascript files) formatting out of your Perl code.

        *EDIT because I forgot to put this back in* To reference the answer from the Javascript in the HTML you're creating, you'll need, ironically, to set the Value of the textfield to equal the value of the Javascript -- although there are other ways, this might be the cleanest. You would do something like document.getElementById("response1”).value = "text_of_answer" to set it, and you can put it where you need to within the Javascript itself.

        Having said that...if you're unfamiliar with Javascript, you're likely best to do some research and testing with static files containing HTML and Javascript, and then come back to implementing CGI. Even if your values are from CGI forms, you can just use a test variable and roll with it.

        You're trying to cover a LOT of newtech in one go, and I think, from reading your past writeups, this might be causing you frustration. Take one chuck of tech (HTML, Javascript, Perl, etc.) at a time, play with them, and you should find great success!

        *Another minute, another EDIT...* Try the new Mozilla Javascript site -- it's basically the replacement for the site in the CGI docs you found was dead.

        Goes off to check into making a small patch to the CGI.pm docs...

        ----Asim, known to some as Woodrow.