Link to home
Start Free TrialLog in
Avatar of jovato
jovato

asked on

Help needed with hidden fields and current date

I am trying to use a hidden field in a form, but I am trying to get it to automaticly generate the date. I am using the following line:

<FORM METHOD="POST" ACTION="/cgi-bin/tester.pl">
<b><font face="Verdana" size="2">KBID</font></b>:</P>
<BLOCKQUOTE>
<P>
<INPUT TYPE="hidden" NAME="kbid" value="%Y%m%d">

I have used value= 'date + "%y%m%d"'> as well but nothing works. I use the above in a cgi in the follwoing manner:


<b><font face="Verdana" size="2">KBID</font></b>: </P>$param{kbid}</font></P>

But it just prints either date + --or-- %y%m%d....  Can I use year month day in a hidden field? Please let me know how I can get the above done.

Thanks!
Avatar of BassTeQ
BassTeQ
Flag of Australia image

Add this code to your program

#date stuff
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$mon=$mon+1;
$year=$year+1900 if ($year >= 100);
$currdate="$mday/$mon/$year";

Then change this line
<INPUT TYPE="hidden" NAME="kbid" value="%Y%m%d">

to this
<INPUT TYPE="hidden" NAME="kbid" value="$currdate">

You can change the $currdate variable to display the date format you require.
eg
$currdate="$mday-$mon-$year";
$currdate="$mday"."$mon"."$year";
etc ...

Hope this helps!
Avatar of Tintin
Tintin

BassTeQ.

$year=$year+1900 if ($year >= 100);

No need for the if test.

$year+=1900;  

will *always* be accurate for any dates >= 1900

Also no need to set a whole bunch of variables that aren't used in:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;

better to do:

($mday,$mon,$year) = (localtime)[3,4,5];

And a neater way of doing the whole lot is:

use POSIX;
$date = strftime("%Y/%m/%d",localtime);


jovato.

In order to get the form to put in the date, you will need to use javascript, unless you are automatically generating the form with a language such as Perl.
A few good points there tintin, thats just the standard code I use for all my date/time functions, thats why I declare everything, never know when you're going to need it :)

Cheers
$param{kbid}= '<INPUT TYPE="hidden" NAME="kbid" value="'
            . `date +"%Y%m%d"` . '">';
You can change your .html to .shtml to utilize the "server side includes" and use the following code.

<!--#config timefmt="%Y%m%d" -->

<INPUT TYPE="hidden" NAME="kbid"
value='<!--#echo var="DATE_LOCAL" -->'>
Avatar of jovato

ASKER

I have tried the above comments and none work. Let me add a little more:

I am bouncing this form off of a perl script. This is for a knowledgebase. All I am tring to do is automaticly make a knowledgebase number for the html document that the perl script kicks out. For example: A user enters all of the relevent data into the fields and hits submit. At that time the script takes the current date and the script uses it to create a name for the page. So when a user hits submit, the parameter kbid should be like 022320030845.html. (month day year hour minute). I just need a way to configure this either on the form itself (as a hidden field) or within the script.
> At that time the script takes the current date
does this mean that the time should be created at client side?
If so, you need JavaScript(or any anyother active scripting language).
Are you aware that such a solution might create multiple identical timestamps?
Avatar of jovato

ASKER

Good point. I would rather see the time be server side. Seems like it would be easier as well.
Avatar of jovato

ASKER

Here is the perl script:

sh-2.05$ more tester.pl
#!/usr/bin/perl

use CGI;
use CGI::Carp qw(fatalsToBrowser);


my $q = new CGI;
print $q->header;
my %param = $q->Vars;



my $template = <<"EOF";
<html>
<head>
<title>cpconfig.com KB: $param{issue}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#FF0000" vlink="#FF0000" alink="#FF
0000">
<center>
<table width="600" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td width="600" height="550" valign="top">
      <div align="center">
        <p><b><font face="Verdana, Arial, Helvetica, sans-serif" size="4" color="#999999"><br>
        </font><font face="Fixedsys" size="6" color="#000000">cpconfig</font></b></p>
      </div>
      <div align="center">
        <b><font face="Fixedsys" size="2">KnowledgeBase</font></b></div>
      <blockquote>
        <div align="center">
          <hr>
          <font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#999999">&nbsp;|
          <a href="#">cpconfig forum</a> | <a href="#">Home</a>
          | <a href="#">Documentation</a> |
          <a href="#">FAQ</a> | <a href="#">Links</a> | <a href="#">KB Help</a> |</font><div align="left">
            <hr>
            &nbsp;
            <p>
<b><font face="Verdana" size="2">KBID</font></b>: </P>$param{kbid}</font></P>

            <font face="Verdana" size="2"><b>Issue:</b></font>
<p><font face="Verdana" size="2">$param{issue}</font></P>
            <p><font face="Verdana" size="2"><b>Version:</b></font></P>
<p><font face="Verdana" size="2">$param{version}</font></P>
            <font face="Verdana" size="2"><b>Platform:</b></font>
<p><font face="Verdana" size="2">$param{platform}</font></P>
            <p><font face="Verdana" size="2"><b>Abstract:</b></font></P>
<p><font face="Verdana" size="2">$param{abstract}</font></P>
            <font face="Verdana" size="2"><b>Solution:</b></font>
<p><font face="Verdana" size="2">$param{solution}</font></P>
            <p><font face="Verdana" size="2"><b>Links/Sources:</b></font></P>
<p><font face="Verdana" size="2">$param{links}</font></P>
            <font face="Verdana" size="2"><b>Submitted By:</b></font>
<p><font face="Verdana" size="2">$param{submitted}</font></P>
            <p>&nbsp;</p>
            <p><font face="Verdana, Arial, Helvetica, sans-serif" color="#999999" size="1"><i>
            DISCLAIMER: The
            following information is believed to be accurate. If you find
            anything wrong with any of the documentation, please e-mail us at
            <a href="mailto:docs\@cpconfig.com?subject=KnowledgeBase problems">kb\@cpconfig.com</a> and let us know. We take n
o responsibility for
            the accuracy or completeness of the documentation here. As always,
            please test everything in a lab environment and not in production.</i></font></p>
            <p>&nbsp;</p>
          </div>
        </div>
      </blockquote>
    </td>
  </tr>
</table></center>
</body>
</html>
EOF


open (FILE, ">/usr/local/apache/htdocs/knowledgebase/$param{kbid}.html")  or die "Can not open $param{kbid}.html $!\n";
print FILE $template;
close (FILE);

print "<h1>Created File</h1>";
exit;
sh-2.05$
Avatar of jovato

ASKER

And here is the form:

sh-2.05$ more blank_kb.htm
<html>
<head>
<title>Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!--#config timefmt="%Y%m%d" -->

</head>

<body bgcolor="#FFFFFF" text="#000000" link="#FF0000" vlink="#FF0000" alink="#FF
0000">
<center>
  <table width="600" border="1" cellpadding="0" cellspacing="0">
    <tr>
      <td width="600" height="550" valign="top">
        <div align="center">
          <p><b><font face="Verdana, Arial, Helvetica, sans-serif" size="4" colo
r="#999999"><br>
          </font><font face="Fixedsys" size="6" color="#000000">cpconfig</font><
/b></p>
        </div>
        <div align="center">
          <b><font face="Fixedsys" size="2">KnowledgeBase</font></b></div>
        <blockquote>
          <div align="center">
            <hr>
            <font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#999999">&nbsp;|
            <a href="/forums/index.php">cpconfig forum</a> | <a href="/">Home</a>
            | <a href="/kbhelp.htm">Documentation</a> |
            <a href="/faq.htm">FAQ</a> | <a href="/links.htm">Links</a> | <a href="#">KB Help</a> |</font><div align="left">
              <hr>
              <P>
              <FORM METHOD="POST" ACTION="/cgi-bin/tester.pl">
<b><font face="Verdana" size="2">KBID</font></b>:</P>
<BLOCKQUOTE>
<P>
<INPUT TYPE="hidden" NAME="kbid" value='<!--#echo var="DATE_LOCAL" -->'>


<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font face="Verdana" size="2">Issue</font></b>:</P>
<BLOCKQUOTE>
<P>
<TEXTAREA NAME="issue" ROWS=5 COLS=43></TEXTAREA>
<BR>
</P>
</BLOCKQUOTE>
<P>
<font face="Verdana" size="2"><b>Version</b></font>:</P>
<BLOCKQUOTE>
<P>
<INPUT TYPE=TEXT NAME="version" SIZE=51>
<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font face="Verdana" size="2">Platform:</font></b></P>
<BLOCKQUOTE>
<P>
<INPUT TYPE=TEXT NAME="platform" SIZE=51>
<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font face="Verdana" size="2">Abstract:</font></b></P>
<BLOCKQUOTE>
<P>
<TEXTAREA NAME="abstract" ROWS=5 COLS=44></TEXTAREA>
<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font size="2" face="Verdana">Solution:</font></b></P>
<BLOCKQUOTE>
<P>
<TEXTAREA NAME="solution" ROWS=5 COLS=44></TEXTAREA>
<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font size="2" face="Verdana">Links/Sources:</font></b></P>
<BLOCKQUOTE>
<P>
<TEXTAREA NAME="links" ROWS=5 COLS=44></TEXTAREA>
<BR>
</P>
</BLOCKQUOTE>
<P>
<b><font size="2" face="Verdana">Submitted By:</font></b></P>
<BLOCKQUOTE>
<P>
<INPUT TYPE=TEXT NAME="submitted" SIZE=53>
<BR>
</P>
</BLOCKQUOTE>
              <p>
<INPUT TYPE=SUBMIT VALUE="Submit Form">
<INPUT TYPE=RESET VALUE="Reset Form"> </p>
</FORM>
              <p>&nbsp;</p>
              <p><font face="Verdana, Arial, Helvetica, sans-serif" color="#999999" size="1"><i>
              DISCLAIMER: The
              following information is believed to be accurate. If you find
              anything wrong with any of the documentation, please e-mail us at
              <a href="mailto:docs@cpconfig.com?subject=KnowledgeBase problems">kb@cpconfig.com</a> and let us know. We take n
o responsibility for
              the accuracy or completeness of the documentation here. As always,
              please test everything in a lab environment and not in production.</i></font></p>
              <p>&nbsp;</p>
            </div>
          </div>
        </blockquote>
      </td>
    </tr>
  </table></center>
</body>
</html>
Change your perl script to be.
You can chage the date stuff with the suggestions Tintin made if you wish.


#!/usr/bin/perl

use CGI;
use CGI::Carp qw(fatalsToBrowser);

#date stuff
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$mon=$mon+1;
$year=$year+1900 if ($year >= 100);
$currdate="$mon"."$mday"."$year"."$hour"."$min";


my $q = new CGI;
print $q->header;
my %param = $q->Vars;



my $template = <<"EOF";
<html>
<head>
<title>cpconfig.com KB: $param{issue}</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#FF0000" vlink="#FF0000" alink="#FF
0000">
<center>
<table width="600" border="1" cellpadding="0" cellspacing="0">
 <tr>
   <td width="600" height="550" valign="top">
     <div align="center">
       <p><b><font face="Verdana, Arial, Helvetica, sans-serif" size="4" color="#999999"><br>
       </font><font face="Fixedsys" size="6" color="#000000">cpconfig</font></b></p>
     </div>
     <div align="center">
       <b><font face="Fixedsys" size="2">KnowledgeBase</font></b></div>
     <blockquote>
       <div align="center">
         <hr>
         <font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#999999">&nbsp;|
         <a href="#">cpconfig forum</a> | <a href="#">Home</a>
         | <a href="#">Documentation</a> |
         <a href="#">FAQ</a> | <a href="#">Links</a> | <a href="#">KB Help</a> |</font><div align="left">
           <hr>
           &nbsp;
           <p>
<b><font face="Verdana" size="2">KBID</font></b>: </P>$param{kbid}</font></P>

           <font face="Verdana" size="2"><b>Issue:</b></font>
<p><font face="Verdana" size="2">$param{issue}</font></P>
           <p><font face="Verdana" size="2"><b>Version:</b></font></P>
<p><font face="Verdana" size="2">$param{version}</font></P>
           <font face="Verdana" size="2"><b>Platform:</b></font>
<p><font face="Verdana" size="2">$param{platform}</font></P>
           <p><font face="Verdana" size="2"><b>Abstract:</b></font></P>
<p><font face="Verdana" size="2">$param{abstract}</font></P>
           <font face="Verdana" size="2"><b>Solution:</b></font>
<p><font face="Verdana" size="2">$param{solution}</font></P>
           <p><font face="Verdana" size="2"><b>Links/Sources:</b></font></P>
<p><font face="Verdana" size="2">$param{links}</font></P>
           <font face="Verdana" size="2"><b>Submitted By:</b></font>
<p><font face="Verdana" size="2">$param{submitted}</font></P>
           <p>&nbsp;</p>
           <p><font face="Verdana, Arial, Helvetica, sans-serif" color="#999999" size="1"><i>
           DISCLAIMER: The
           following information is believed to be accurate. If you find
           anything wrong with any of the documentation, please e-mail us at
           <a href="mailto:docs\@cpconfig.com?subject=KnowledgeBase problems">kb\@cpconfig.com</a> and let us know. We take n
o responsibility for
           the accuracy or completeness of the documentation here. As always,
           please test everything in a lab environment and not in production.</i></font></p>
           <p>&nbsp;</p>
         </div>
       </div>
     </blockquote>
   </td>
 </tr>
</table></center>
</body>
</html>
EOF


open (FILE, ">/usr/local/apache/htdocs/knowledgebase/$currdate.html")  or die "Can not open $currdate.html $!\n";
print FILE $template;
close (FILE);

print "<h1>Created File</h1>";
exit;
Avatar of jovato

ASKER

BassTeQ,

I made the changes above, but the files are being saved as $currdate.html. The script modifiactions are exactly as you specified. Can you think of any reason this wouldn't work?
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jovato

ASKER

That was the ticket.