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.

Most Commented Posts