Automatic date fill

liunx

Guest
Hi Guys,<br />
<br />
I have a form with a date field. I'd like the field to self-populate. I already have a script that writes the date, but I can't seem to get it to write the date inside the field, so it's not being passed to the database. Can someone help?<br />
<br />
Here's the code for the field (very basic):<br />
<input name="Date" type="text" id="date"><!--content-->What server side language are you using? Are you just using cgi with a form action?<!--content--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"<br />
"http://www.w3.org/TR/html4/strict.dtd"><br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><br />
<meta name="Content-Script-Type" content="text/javascript"><br />
<title>Example</title><br />
<br />
<style type="text/css"><br />
<!-- <br />
label {display:block; margin:1em 0em}<br />
input {display:block} <br />
--><br />
</style><br />
<br />
<script type="text/javascript"><br />
<!--<br />
onload = function () {document.forms[0].date.value = new Date().toDateString()}<br />
// --><br />
</script><br />
<br />
<form action=""><br />
<div><br />
<label>Date<input name="date" type="text"></label><br />
<button type="submit">Submit</button><br />
</div><br />
</form><br />
<br />
But yes, JavaScript will fail 13% of the time. So if you cannot count on those users properly filling in the date field then you will have to use a server side method.<!--content-->Just using cgi with form action.<br />
<br />
Charles,<br />
Thanks once again. I'll wrestle with what you've given me here & see if I can make it work. I've never seen this <label> tag before.<br />
<br />
We'll just have to deal with the 13%... (Does anybody else think that would make a good sig?)<br />
<br />
Aronya1<!--content-->Originally posted by Aronya1 <br />
I've never seen this <label> tag beforeThen see <!-- m --><a class="postlink" href="http://www.w3.org/TR/html4/interact/forms.html#edef-LABEL">http://www.w3.org/TR/html4/interact/for ... edef-LABEL</a><!-- m -->. />
<br />
Your script works fine on it's own, but how do I integrate into my form? The best I can do is put your onload line into the <head> of my document. Then the page loads fine, but the date doesn't display.<br />
<br />
If I put the code into the body of my document, I get an error: 'document.forms.0.date' is null or not an object.<br />
<br />
I'm reasonably sure the goof is mine, but I don't understand javascript, either. Any help would be appreciated.<br />
<br />
thanks.<!--content-->1) Make sure that the form in question is the first one on the page. If not, then increase accordingly the index applied to the "forms" array.<br />
<br />
2) Make sure that your field is named "date" and note that JavaScript is case sensitive.<!--content-->
 
Back
Top