Click to See Complete Forum and Search --> : JS getFullYear() Error


kwilliams
09-30-2003, 11:39 AM
Hello,

Here's the basics of my problem, and I'm hoping that someone sees something simple in what I'm doing wrong:

IN <HEAD> OF DOC:
<script type="text/javascript">
function year(){
var d = new Date();
var yr = d.getFullYear();
document.write(yr);}
</script>

IN <BODY> OF DOC:
<input type="hidden" name="hfYear" value="year()">

RESULTS IN:
2003undefined

I of course only need the full year of 2003. Can anyone see what I'm doing wrong. Any and help is appreciated. Thanks.

KWilliams

gil davis
09-30-2003, 12:14 PM
When you execute a document.write(), it will usually wipe out the previous content of the document, making things undefined. So it would not surprise me to see something mysterious happen.

Khalid Ali
09-30-2003, 12:25 PM
Here it seems like you want to set the hidden elements value to be the full year, this script below should help

<body onload="var d =new Date();document.getElementById('fullyear').value=d.getFullYear();">
<input type="hidden" id="fullyear"/>
<input type="button" value="Test Full Year" onclick="alert(document.getElementById('fullyear').value)"/>

Charles
09-30-2003, 12:57 PM
And we can shorten that up a bit by using...

<body onload="document.getElementById('fullyear').value = new Date().getFullYear()">

kwilliams
09-30-2003, 01:49 PM
Wow, a lot of quic replies....thanks.

Well, I tried your suggestion of:
<body onload="document.getElementById('fullyear').value = new Date().getFullYear()">
<input type="hidden" name="hfYear" id="fullyear">

..but this just inserted "fullyear" into the DB table under Year. The hiddenfield is named "HfYear", and this page is using an RTEfrom DMXZone. I thought that this should be pretty straight forward, but it's getting pretty complicated to figure out.

The INSERT statement is looking for the value of the hiddenfield, so I tried several values from your suggestion (including fullyear), but none of them worked. The "id" didn't work as the value either.

Do you have any suggestions what I should put as the value of the hiddenfield to make this insert properly? Thanks for your help.

KWilliams

Khalid Ali
09-30-2003, 02:30 PM
The suggestion I posted presumes that you are not posting this data to a webserver to be inserted in to a db,if it is then it will not work,since unless you use a form element and properly submit that form to the db app it will not get the value from the hidden field.

now in case you are doing exactly teh same as I understood above?..code .below will work

<body onload="var d =new Date();document.getElementById('fullyear').value=d.getFullYear();">
<form id="form1" action="http://databaseapplicationsURL.jsp" onsubmit="" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" id="fullyear"/>
<input type="submit" value="Test Full Year" onclick="alert(document.getElementById('fullyear').value)"/>
</form>

make sure that action attribute points to the correct database processing application.

kwilliams
09-30-2003, 02:38 PM
Khalid,

I just tried your suggestion of:
<body onload="var d=new Date();document.getElementById('hfYear').value=d.getFullYear();">
<form ACTION="<%=MM_editAction%>" METHOD="POST" id="form1" name="form1">
<input type="hidden" name="hfYear" id="hfYear">

...but nothing got entered into the database. I think that I'm going to give up on this one for now, as I can use other methods to get the year (as I have a separate date column in the DB). Thanks for your help...I really appreciate it.

KWilliams

Khalid Ali
09-30-2003, 03:36 PM
I guess you are right,however,id the above did not work then it means( I am very much confident) that the problem is in your code where you are getting data from the form..

:D