By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,828 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,828 IT Pros & Developers. It's quick & easy.

Passing values between HTML forms

P: n/a
Hello,

I am stumped on how to set a form field value to a value in a
different form. It seems like something that should be pretty easy.
Basic example below:

<form name = "form1" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname"
id="fname" />
</form>

<form name = "form2" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname" id="fname"
value = ??? />
</form>

basically, I want the fname field in form2 to be equal to the fname
field in form1, but can't find the syntax. Any help on what goes in
the value attribute for the 2nd form? Thanks in advance.

(Sorry for posting this in the javascript forum, I could not find an
html forum that looked like it had any activity. There is javascript
elsewhere in my page)

Feb 12 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
robkiolbasa wrote:
Hello,

I am stumped on how to set a form field value to a value in a
different form. It seems like something that should be pretty easy.
Basic example below:

<form name = "form1" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname"
id="fname" />
</form>

<form name = "form2" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname" id="fname"
value = ??? />
</form>

basically, I want the fname field in form2 to be equal to the fname
field in form1, but can't find the syntax. Any help on what goes in
the value attribute for the 2nd form? Thanks in advance.

(Sorry for posting this in the javascript forum, I could not find an
html forum that looked like it had any activity. There is javascript
elsewhere in my page)
That is OK, you need Javascript to do what you want, so this is the right
place.

The get the value of the first form:

var theName = document.forms.form1.fname.value;

To place that in form2.fname:
document.forms.form2.fname.value = theName;

Or in 1 go:
document.forms.form2.fname.value = document.forms.form1.fname.value;

But I don't know WHEN you want to fire that action.
If you want that to happen when the first form has its field changed, use
the onChange handler, like something like this:

<form name="form1" action="nothing.asp" method="POST">
name: <input name="fname" id="fname" value="Type your name here"
onChange='doSomething();'>
</form>

<form name = "form2" action="nothing.asp" method="POST">
<input name="fname" id="fname" value="">
</form>

<script type="text/javascript">
function doSomething(){
document.forms.form2.fname.value = document.forms.form1.fname.value;
}
</script>

Not tested, possibility of typos is huge. :-)

Hope that helps you going.

Regards,
Erwin Moller
Feb 12 '07 #2

P: n/a
On Feb 12, 10:21 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
robkiolbasa wrote:
Hello,
I am stumped on how to set a form field value to a value in a
different form. It seems like something that should be pretty easy.
Basic example below:
<form name = "form1" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname"
id="fname" />
</form>
<form name = "form2" action = "nothing.asp" method="POST" >
<label for="fname">First Name*:</label<input name="fname" id="fname"
value = ??? />
</form>
basically, I want the fname field in form2 to be equal to the fname
field in form1, but can't find the syntax. Any help on what goes in
the value attribute for the 2nd form? Thanks in advance.
(Sorry for posting this in the javascript forum, I could not find an
html forum that looked like it had any activity. There is javascript
elsewhere in my page)

That is OK, you need Javascript to do what you want, so this is the right
place.

The get the value of the first form:

var theName = document.forms.form1.fname.value;

To place that in form2.fname:
document.forms.form2.fname.value = theName;

Or in 1 go:
document.forms.form2.fname.value = document.forms.form1.fname.value;

But I don't know WHEN you want to fire that action.
If you want that to happen when the first form has its field changed, use
the onChange handler, like something like this:

<form name="form1" action="nothing.asp" method="POST">
name: <input name="fname" id="fname" value="Type your name here"
onChange='doSomething();'>
</form>

<form name = "form2" action="nothing.asp" method="POST">
<input name="fname" id="fname" value="">
</form>

<script type="text/javascript">
function doSomething(){
document.forms.form2.fname.value = document.forms.form1.fname.value;
}
</script>

Not tested, possibility of typos is huge. :-)

Hope that helps you going.

Regards,
Erwin Moller- Hide quoted text -

- Show quoted text -
Got it working, thanks Erwin.

Feb 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.