- #!/usr/bin/perl -w
- use CGI::FormBuilder;
- my $docroot = '../build/html';
- # TODO: check if protocol-agnostic URL works
- my $webroot = 'https://form.omni-presence.dk/form';
- my $req_path = $docroot . '/index.tmpl';
- my $ack_path = $docroot . '/thanks/index.html';
- my $webmaster = 'webmaster@omni-presence.dk';
- my $frontdesk = 'info@omni-presence.dk';
- my $helpdesk = 'dr@jones.dk';
- # Set this to 1 for a separate confirmation page
- my ($confirm) = 1;
- # Built-in email validation is too simplistic
- my $valid_email = '/^[+_A-Za-z0-9-]+(\.[+_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[_A-Za-z0-9-]+)$/';
- my $form = CGI::FormBuilder->new(
- lang => 'en',
- title => 'Omni-presence contact form',
- method => 'POST',
- fields => [qw/
- name
- email
- human
- eyes
- comment
- /],
- validate => {
- email => $valid_email,
- _email => $valid_email, #fake check to silence warning of variable used only once
- },
- required => [qw/
- name
- email
- human
- /],
- messages => ':en_US',
- submit => ['Submit', 'Submit via secure connection'],
- action => $webroot, #avoids loosing submitted values when redirected from other site
- );
- $form->field(
- name => 'name',
- class => 'form-control',
- message => '- Please provide your name',
- );
- $form->field(
- name => 'email',
- id => 'inputEmail',
- class => 'form-control',
- placeholder => 'Email',
- message => '- Please provide your email address',
- );
- $form->field(
- name => 'human',
- label => '',
- class => 'checkbox',
- options => ["I'm not a robot!"],
- multiple => 0,
- message => '- Sorry, no robots allowed!',
- );
- $form->field(
- name => 'eyes',
- label => 'eye color',
- class => 'form-control',
- type => 'select',
- options => [qw(blue green other)],
- );
- $form->field(
- name => 'comment',
- class => 'form-control',
- type => 'textarea',
- rows => 5,
- );
- my ($form_required_text) = $form->{opt}{messages}{form_required_text};
- my ($mail_from) = $webmaster;
- my ($mail_to, $mail_subject);
- if ($form->submitted) {
- $infostring = "Thanks for your interest in omni-presence!";
- if ($form->validate) {
- $mail_to = $frontdesk;
- $mail_subject = 'Omni-presence contact form';
- } else {
- $mail_to = "$frontdesk, $helpdesk";
- $mail_subject = 'ERROR in omni-presence contact form';
- $infostring .= "\n<P>NB! There was an error in one or more of the fields. The info was sent anyway, but you are adviced to go back, check that all fields are properly filed out, and then send again.";
- $confirm = 0;
- };
- # Abuse subject to add additional headers
- # $mail_subject .= "\n" . 'Content-Type: text/plain; charset="ISO-8859-1";';
- # $mail_subject .= "\n" . 'Content-Transfer-Encoding: 8bit';
- $form->mailresults(
- to => $mail_to,
- from => $mail_from,
- subject => "$mail_subject",
- );
- if ($confirm) {
- $infostring .= "\n<P>The following info has been submitted:";
- $form->{opt}{messages}{form_confirm_text} = "$infostring";
- print $form->confirm(
- header => 1,
- template => $ack_path
- );
- } else {
- printhack( $form->render(
- header => 1,
- sticky => (! $form->validate),
- #FIXME text => $infostring,
- template => $req_path
- ));
- }
- } elsif ($ENV{'FORMBUILDER_NOHEADER'}) {
- printhack( $form->render(
- header => 0,
- template => $req_path
- ));
- } else {
- printhack( $form->render(
- header => 1,
- template => $req_path
- ));
- }
- # strip div around checkbox field, to support contextually hiding next field
- sub printhack {
- $_ = shift;
- s,<div class="">,,g;
- s,</div>\s*(?=<div class="eye color">),,s;
- print $_;
- }
|