Click to See Complete Forum and Search --> : Automatic date fill
Aronya1
09-13-2003, 08:04 PM
Hi Guys,
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?
Here's the code for the field (very basic):
<input name="Date" type="text" id="date">
PeOfEo
09-13-2003, 08:08 PM
What server side language are you using? Are you just using cgi with a form action?
Charles
09-13-2003, 08:10 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
onload = function () {document.forms[0].date.value = new Date().toDateString()}
// -->
</script>
<form action="">
<div>
<label>Date<input name="date" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>
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.
Aronya1
09-13-2003, 08:26 PM
Just using cgi with form action.
Charles,
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.
We'll just have to deal with the 13%... (Does anybody else think that would make a good sig?)
Aronya1
Charles
09-13-2003, 08:48 PM
Originally posted by Aronya1
I've never seen this <label> tag beforeThen see http://www.w3.org/TR/html4/interact/forms.html#edef-LABEL.
Aronya1
09-13-2003, 09:25 PM
Charles,
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.
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.
I'm reasonably sure the goof is mine, but I don't understand javascript, either. Any help would be appreciated.
thanks.
Charles
09-14-2003, 07:11 AM
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.
2) Make sure that your field is named "date" and note that JavaScript is case sensitive.