0

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)
      )
    );
Allen
  • 81
  • 1
  • 10
  • 2
    https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used – Quentin Sep 14 '16 at 20:51
  • 2
    You're effectively asking multiple questions here. "How do I put a date time picker in an HTML page?", "How do I generate the markup for that from CGI.pm?" (see previous comment and "don't"). "How do I find the current date and time with Perl?". You should focus on a narrower problem. – Quentin Sep 14 '16 at 20:52
  • 1
    For a datepicker, you'll need JavaScript. I've used [jQuery UI's datepicker](http://jqueryui.com/datepicker/), but there are probably tons of other ones. – ThisSuitIsBlackNot Sep 14 '16 at 21:51
  • @ThisSuitIsBlackNot I like your suggestion, how would you add the datepicker you suggested to an object-oriented CGI page, such as the one I have? – Allen Sep 14 '16 at 22:06
  • 1
    Create a separate JavaScript file and include it in your HTML with a ``. As Quentin said, you shouldn't be using CGI.pm's HTML generation functions. For a simple HTML form, you may not even need to *generate* anything in the first place; just use a static HTML file. If your HTML really needs to be dynamic, you should use a templating tool like [Template Toolkit](http://template-toolkit.org/). – ThisSuitIsBlackNot Sep 14 '16 at 22:12
  • [Crossposted](http://www.perlmonks.org/?node_id=1171784) – choroba Sep 15 '16 at 07:54

2 Answers2

0

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

Community
  • 1
  • 1
RedSands
  • 146
  • 1
  • 13
0

If you know your users are going to be using modern browsers like Edge or Chrome, then you can use the HTML5 date input.

<input type="date" value="2016-09-15">

That gives a nice date-picker on supported browsers and downgrades to a simple text input where it isn't supported.

You can create it with CGI's HTML creation functions.

$q->input({type => 'date', value => '2016-09-15'});

But as others have pointed out, we've known that the HTML creation functions are a stupid idea for at least 15 years. No-one should be using them now. Please use a templating engine instead.

As for getting the date. The easiest option is probably to use Time::Piece.

use Time::Piece;
my $date = localtime;
say $q->input({type => 'date', value => $date->strftime('%Y-%m-%d')});
Dave Cross
  • 62,464
  • 3
  • 46
  • 83