in reply to Re^8: obtaining yyyy-mm-dd via a calendar object
in thread obtaining yyyy-mm-dd via a calendar object

Hi Asim, OK, I'm back, a little beaten up, but much wiser. I have built a calendar widget from scratch using the Calendar::Simple pm. The prg is tested and works predictably (correctly as far as I can tell). When the program is invoked, a calendar for the current month is displayed. The user is allowed to pick a day, or select the previous or next month as desired. When the user selects a day, the program returns the yyyy-mm-dd as a string.
I would like some advice on how to bridge the final gap. How do I get this calendar widget to work within a CGI form?
# Calendar widget program. Creates pop-up calendar to select a date. package CalendarWidget; use strict; #force all variables to be declared before use use CGI qw(:standard escape escapeHTML); # most common CGI interface f +unctions use CGI::Carp qw(warningsToBrowser fatalsToBrowser); #all diags to bro +wser use Calendar::Simple; my $requested_new_month = param("requested_new_month"); my $specific_date_was_selected = param("specific_date_was_selected"); my $yyyy = param("yyyy"); my $mm = param("mm"); my $dd = param("dd"); sub calendar_widget { ####### dispatching logic begins here if (defined (param("specific_date_was_selected"))) #spec date was sele +cted { if ($specific_date_was_selected eq "specific_date_was_selected") {return($yyyy,$mm,$dd);} # return selected date to calling pr +ogram else {} } elsif (defined (param("requested_new_month"))) # if request not curren +t mo & yr { if ($requested_new_month eq "previous_month") # requested previous +_month { # adjust calendar backward in time if ($mm eq "1") # if previously displayed month was January { $mm = 12; # change new displayed month to December $yyyy = $yyyy-1; # and adjust year } else # for all other months {$mm = $mm-1; } #change new displayed month to previous mon +th } else # requested_new_month was next_month { # adjust calendar forward in time if ($mm eq "12") # if previously displayed month was December { $mm = 1; # change new displayed month to January $yyyy = $yyyy+1; # and adjust year } else { $mm = $mm+1; } #change new displayed month to next month } show_calendar($mm, $yyyy); #show the revised calendar } else { show_calendar(); } #show calendar using current month / year } ####### dispatching logic ends here sub show_calendar { my ($specified_month, $specified_year) = @_; #get passed parms from ca +lling pgm my (@month, @row, @calendar_table_entry, @array, $array, $url1, $url2, + $theTime); my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my @weekDays = qw (Sun Mon Tue Wed Thu Fri Sat); my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfW +eek, $dayOfYear, $daylightSavings, $year) = localtime(); $year = 1900 + $yearOffset; $theTime = "$weekDays[$dayOfWeek], $months[$month] $dayOfMonth, $year +$hour:$minute:$second"; my @month_names = qw(January February March April May June July August + September October November December); if (($specified_month eq '') || ($specified_year eq '')) #if no date s +pec'ed { @month = calendar($month+1, $year); # uses ACTUAL # of month & yea +r $specified_month = $month+1; $specified_year=$year; } else # (month / year info were spec'ed, get info for specified month / + year { @month = calendar($specified_month, $specified_year); } $url1 = url().sprintf("?requested_new_month=%s&yyyy=%s&mm=%s",escape(" +previous_month"),escape($specified_year),escape($specified_month)); $url2 = url().sprintf("?requested_new_month=%s&yyyy=%s&mm=%s",escape(" +next_month"),escape($specified_year),escape($specified_month)); push (@row,qq(<table border=3 bordercolor="red" align="left" bgcolor=" +x00ffff">) ); my $i=0; my $j=0; @array = @weekDays; $array = param('@array'); push (@row, Tr ( ( qq(<TD ALIGN="CENTER" VALIGN="CENTER", COLSPAN="7">), qq(<FONT CO +LOR=#FF0000>), "$theTime", qq(</FONT>) ) ) ); push (@row, Tr ( ( qq(<TD ALIGN="LEFT" VALIGN="CENTER", COLSPAN="1">), qq(<FONT COLO +R=#0000FF>), (a({-href=>$url1},"<<")), qq(</FONT>) ), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER", COLSPAN="5">), qq(<FONT CO +LOR=#0000FF>), "@month_names[$specified_month-1] $specified_year", qq +(</FONT>) ), ( qq(<TD ALIGN="RIGHT" VALIGN="CENTER", COLSPAN="1">), qq(<FONT COL +OR=#0000FF>), (a({-href=>$url2},">>")), qq(</FONT>) ) ) ); foreach $array(@array) # specify calendar days of week { @calendar_table_entry[$j] = $array; $i++; $j++; } $j=0; push (@row, Tr ( ( qq(<TD ALIGN="RIGHT" VALIGN="CENTER", COLSPAN="1">), @calendar_ta +ble_entry[$j] ), td (@calendar_table_entry[$j+1]), td (@calendar_tabl +e_entry[$j+2]), td (@calendar_table_entry[$j+3]), td (@calendar_table +_entry[$j+4]), td (@calendar_table_entry[$j+5]), td (@calendar_table_ +entry[$j+6]) ) ); foreach (@month) { $j=0; @array = @$_; foreach $array(@array) # specify calendar numbered days in month { @calendar_table_entry[$j] = $array; $i++; $j++; } $j=0; push (@row, Tr ( ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), qq(<FONT COLOR=#FF0000>) +, (a({-href=>url().sprintf("?specific_date_was_selected=%s&yyy +y=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape +($specified_year), escape($specified_month), escape(@calendar_table_e +ntry[$j]))}, @calendar_table_entry[$j])), qq(</FONT>) ), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), (a({-href=>url().sprintf +("?specific_date_was_selected=%s&yyyy=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month),escape(@calendar_table_entry[$j+1]))}, @calendar_ta +ble_entry[$j+1]))), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), (a({-href=>url().sprintf +("?specific_date_was_selected=%s&yyyy=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month), escape(@calendar_table_entry[$j+2]))}, @calendar_t +able_entry[$j+2]))), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), (a({-href=>url().sprintf +("?specific_date_was_selected=%s&yyyy=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month), escape(@calendar_table_entry[$j+3]))}, @calendar_t +able_entry[$j+3]))), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), (a({-href=>url().sprintf +("?specific_date_was_selected=%s&yyyy=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month), escape(@calendar_table_entry[$j+4]))}, @calendar_t +able_entry[$j+4]))), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), (a({-href=>url().sprintf +("?specific_date_was_selected=%s&yyyy=%04d&mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month), escape(@calendar_table_entry[$j+5]))}, @calendar_t +able_entry[$j+5]))), ( qq(<TD ALIGN="CENTER" VALIGN="CENTER">), qq(<FONT COLOR=#FF0000>) +, (a({-href=>url().sprintf("?specific_date_was_selected=%s&yyyy=%04d& +mm=%02d&dd=%02d", escape("specific_date_was_selected"), escape($specified_year), escape( +$specified_month), escape(@calendar_table_entry[$j+6]))}, @calendar_t +able_entry[$j+6])), qq(</FONT>) ) ) ); } print table (@row); return(); } 1;

Replies are listed 'Best First'.
Re^10: obtaining yyyy-mm-dd via a calendar object
by Asim (Hermit) on Aug 21, 2006 at 01:18 UTC
    How do I get this calendar widget to work within a CGI form?

    At a first glance, you need to feed the response into a form variable, so that when you submit the form, the calendar information will be passed into the CGI paramters sent to the form's action target. You'd likely feed the response to the variable via Javascript, but the rest is bog-standard HTML form work.

    Does that make sense? Sorry if it's short, I just got back from vacation...

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

      Welcome back from what is hoped was a relaxing break. OK, we're almost there! Without changing the calendar widget code from Reply 9 in the previous thread, following is the code used to implement your suggestion.
      When the below code executes, it results in an internal server error. Interpreting this to mean that code is not finding the CalendarWidget.pm perl module. Can you discern what my problem could be?
      #! /usr/bin/perl -wT # use strict; #force all variables to be declared before use use CGI qw(:standard escape escapeHTML); # most common CGI interface f +unctions use CGI::Carp qw(warningsToBrowser fatalsToBrowser); #all diags to bro +wser use URI; use lib qw(/home/gmacfadd/public_html/cgi-bin/Calendar); use CalendarWidget; my $selected_date; my ($get_calendar_widget, $item); my $choice = lc (param ("choice")); # get choice, lowercased my $url1 = URI->new('http://www.gmacfadden.com/cgi-bin/Calendar/Calend +arWidget.pm'); my $url_picture = URI->new('http://www.gmacfadden.com/images/calendar. +jpg'); my @field_list = ({ name => "selected_date", label => "Select a date:" }); print header (), start_html (-title => "Test CGI Form", -bgcolor => "o +range"); if ($choice eq "") # initial script invocation { display_entry_form (); } elsif ($choice eq "submit") { process_form (\@field_list); } elsif (defined (param("get_calendar_widget"))) { if ($get_calendar_widget eq "get_calendar_widget") { if ($item eq "item1") { my($y1,$m1,$d1)= CalendarWidget::calendar_widget(); print "<br>You selected $y1-$m1-$d1.<br>\n"; $selected_date = $y1."-".$m1."-".$d1; } else {} } else { } } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html (); exit (0); sub display_entry_form { my @row; # define array print start_form (-action => url ()); #default method is POST push (@row, qq(<table border=3 bordercolor="blue" align="left" + bgcolor="xffff00">) ); push (@row, Tr ( ( qq(<TD ALIGN="CENTER" VALIGN="TOP" BGCOLOR="yellow" COLSPAN="2">) +, qq(Please provide the information requested in the table below.) ) ) ); push (@row, Tr ( ( qq(<TD ALIGN="RIGHT" VALIGN="TOP">), qq(Select a date:) ), ( qq(<TD ALIGN="LEFT" VALIGN="TOP" BGCOLOR="yellow">), (textfield (-name => "selected_date", -size => 10 +)), a({-href=>$url1}, img({-src=>"$url_picture", -alt +=>"click calendar to select a date"}) ) ) )); print table (@row), submit (-name => "choice", -value => "Submit"), end_form (); } sub process_form { my $field_ref1 = shift; # reference to field-List my @errors; $selected_date = param ('selected_date'); foreach my $f (@{$field_ref1}) { if ( ($f->{name} eq "selected_date") ) { push (@errors, $f->{label}) if $selected_date eq +""; #it's empty! } else { } } if (@errors) { print p ("Some information is missing." . " Please fill in the following fields:"); s/:$// foreach (@errors); # strip colons from end of labels print ul (li (\@errors)); # print column names display_entry_form (); # redisplay entry form return; } print p ("The date entered was $selected_date.<br>\n"); }