filling today's date in a form

Tim Chase python.list at tim.thechases.com
Mon Apr 17 10:27:43 EDT 2006


Lawrence D'Oliveiro wrote:
> In article <e1ujkb$438q$1 at netnews.upenn.edu>, Kun <neurogasm at gmail.com> 
> wrote:
> 
> 
>>... but i am 
>>wondering if there is a way to create a button which would automatically 
>>insert today's date in the date form field if the user chooses to use 
>>today's date.
> 
> 
> If you're going to have a button to do it, then the button might as well 
> invoke a JavaScript sequence to fill in the field. No sense bothering 
> the server with something this simple.

If JavaScript is enabled, the following example will do what 
the OP wants:

<html>
<head>
<script type="text/javascript">
function getDate()
{
alert("getting date...");
var d = new Date();
var s = d.toLocaleString();
alert(s);
return s;
}
function setDate()
{
alert("Setting date");
document.getElementById("myDate").value = getDate();
}
</script>
</head>

<body>
<form name="form1">
Date: <input type="text" id="myDate" size="20" />
<br /><br />
<input type="button" value="Today's Date" onclick="setDate();">
</form>
</body>

</html>



However, if JS is not enabled (as I often surf, given how 
requisite JS is for many attack vectors and popups...thank 
goodness for the NoScript plugin for FireFox which allows me 
to whitelist sites allowed to use JS), your only hope is to 
round-trip the server where your CGI/mod_python/whatever 
script is sitting generating the page.  Or populate the 
field by default (using 'value="1/2/03"' attribute on your 
textbox) when creating the field and let the user change it 
if needed.  The ideal is to prepopulate the field to the 
most frequently used value so that the user has as little 
work to do as possible.

You'll find some great tools on JavaScript over at the 
http://www.w3schools.com site where you can experiment with 
their TryIt editor.  That will at least allow you to tinker 
with the JS.  You haven't provided enough details regarding 
your web-scripting environment (Django, CGI, TG, mod_python...)

-tkc








More information about the Python-list mailing list