Passing values between HTML forms

R

robkiolbasa

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)
 
E

Erwin Moller

robkiolbasa said:
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
 
R

robkiolbasa

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.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top