Mar
24th

Post Date with JavaScript

You can use Javascript to post hidden variables, such as Date, to another system when building a Web Form. Here’s an example:

In the <head> of your HTML:

<script language=”javascript”>
function addDate {
var date=new Date();
var month=date.getMonth() + 1;
var day=date.getDate();
var year=date.getYear();
document.myForm.myDate.value = month+”/”+day+”/”+year; return true;
}
</script>

Within the <form> tag:

<form action=”pagetopostto.php” name=”myForm” method=”post” onsubmit=”return addDate();”>

And within the form elements:

<input type=”hidden” name=”myDate”>

How does this work? From the bottom up…

  1. There’s an empty variable called DateSubscribed in the form.
  2. On the submission of the page, the addDate Javascript function is called.
  3. That takes today’s date and sets the value of DateSubscribed to it. The function returns True to the form, letting it know that it can be submitted.

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!