javascript in form

C

Chris

I'm trying to enter a date in a hidden field in a form. Below is the
code that appears in between <form></form>. I think the problem is
the document.write but am not sure. How should I be writing this part
of the code. Thanks, Chris

<SCRIPT LANGUAGE="text/javascript">
<!--
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(<input type="hidden" name="date2" value="curr_year +
"-" + curr_month + "-" + curr_date">);
//-->
</SCRIPT>
 
D

David Dorward

Chris said:
I'm trying to enter a date in a hidden field in a form. Below is the
code that appears in between <form></form>. I think the problem is
the document.write but am not sure.
<SCRIPT LANGUAGE="text/javascript">

The language attribute for the script element is deprecated, if you do have
it, the correct value is "javascript". The type attribute is required (and
it should be text/javascript).

These comments are worthless at best, and can cause problems (especially
when XHTML enters the picture, as it so oftendoes).
document.write(<input type="hidden" name="date2" value="curr_year +
"-" + curr_month + "-" + curr_date">);

The write method takes a string as a parameter. You can't put raw HTML
there.

Why are you doing this though? JavaScript is hardly universal, and getting
the date from the server that your form handler runs on is good enough for
most people's purposes.
 
A

Andrew Bailey

Chris said:
I'm trying to enter a date in a hidden field in a form. Below is the
code that appears in between <form></form>. I think the problem is
the document.write but am not sure. How should I be writing this part
of the code. Thanks, Chris

<SCRIPT LANGUAGE="text/javascript">
<!--
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(<input type="hidden" name="date2" value="curr_year +
"-" + curr_month + "-" + curr_date">);
//-->
</SCRIPT>

Hi Chris,

I'm no expert but when I use document.write I always use this syntax...

document.write('some text');

notice the enclosing '

the next step is...

document.write ('<input type="hidden" name="date2" value="place-holder">');

remember the enclosing '

so the final draft is...

document.write ('<input type="hidden" name="date2" value="' +curr_year+ '-'
+curr_month+ '-' +curr_date+ '">');

so (within the document.write brackets) everything within a pair of ' is
text and everything outside is javascript, the trick is learning how to drop
out of text and back again...

.....some-text' <-end +variable+ start-> 'more-text...


Hope this helps

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top