• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set form field values using javascript?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The following code is on my second page. There is a form field called
time, I want to set it to user's current time after user hits submit button on the first page and comes to this page. I use javascript code like the following
to do that. But it seems it never sets the time value after I come to second page:

<script language="JavaScript">
times=new Date();
document.form2.time.value = times;
</script>
<form name="form2" action="nextPage.php" method="post">
<table>
<tr>
<td> Name:<input id="name" size="10" name="name"></td>
<td> Phone:<input id="phone" size="10" name="phone"></td>
<td> Zip Code:<input id="zipcode" size="5" name="zipcode"></td>
<td> Address:<input id="address" size="30" name="address"></td>
<td> Time:<td><input name="time" value="" readonly /></td>
</tr>
<tr>
<td><input type="button" value="Submit" name="Submit2"></td>
</tr>
</table>

I am wondering what I did wrong here?

thanks,
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are setting the field before it is even exists on the page.

It is like going to a restuarant and they call your name before you even leave to go there.

You need to set it onload or after the control os rendered.

Eric
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, since you are doing nothing with request params on this page, the fact that it is a "second page" is moot.

But take a look at your markup. Where is the reference to the input field? Now where is the input field defined?

Anything clicking?
 
Bear Bibeault
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric beat me to the punch...

You would also be well-served to debug your code in Firefox where the Javascript console would have let you know that there was a problem.
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for giving me pointers.

Unfortunately, even after I set onload event on the body tag, it still does
not work.

I guess something must be wrong somewhere else.
 
Bear Bibeault
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Show us your new code. Please be sure to use UBB code tags when posting.
 
reply
    Bookmark Topic Watch Topic
  • New Topic