Allen Allen - 2 months ago 22
Perl Question

Perl CGI Date and Time picker with auto populate with current date and time

I've created a CGI form using perl, everything works really good, except I'm having a hard time finding a solution as to how I can get the current time and date filled in the form while giving the user the option to change the input with a date and time picker. here is pieces of code I've written:

sub output_form {
my ($q) = @_;
print $q->start_form(
-name => 'main',
-method => 'POST',
);
print $q->start_table;
print $q->Tr(
$q->td('Update#:'),
$q->td(
$q->textfield(-name => "update_num", -size => 02)
)
);
print $q->Tr(
$q->td('Date:'),
$q->td(
$q->textfield(-name => "date", -size => 50)
)
);
print $q->Tr(
$q->td('Time:'),
$q->td(
$q->textfield(-name => "time", -size => 50)
)
);

Answer

Add http://jqueryui.com/datepicker/ to your html page.

print "<link rel='stylesheet' href='//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css'>";
print "<script src='//code.jquery.com/jquery-1.12.4.js'></script>";
print "<script src='//code.jquery.com/ui/1.12.0/jquery-ui.js'></script>";

...

Then add the ID tag to your textfield.

print $q->Tr(
      $q->td('Date:'),
      $q->td(
        $q->textfield(-name => "date", -size => 50, -id => "datepicker")
      )
    );

You might also find this reference helpful from perl monks on using javascript with perl: http://www.perlmonks.org/?node_id=1050387

Here another conversation on the topic of data pickers. jQuery date/time picker

Comments