Need to get a value in a form?

C

cmc_dermo

I have a form that has a select list.

A user chooses a value and the page refreshes showing the selected
value in the dropdown box.

So I want to use Javascript to get the selected query from the form.

I then want to pass it as a hidden field.

So for example the section of the page I'm interested in will look like
this.

<form action="asubmitpage.htm" method="post">

<select name="ent" onchange="form.submit();">
<option value="UK">UK</option>
<option selected value="USA">USA</option>
<option value="JAPAN">JAPAN</option>
<option value="UK">UK</option>
</select>

I then want to take the selected value and submit it as a hidden field.

<form action="setpov.htm" method="post">
<input type="hidden" name="Entity" value=" this is where i want to fill
this with the selected value ">
</form>

I take it javascript is the easiest way to do this.

Regards

Craig
 
K

kaeli

I have a form that has a select list.

A user chooses a value and the page refreshes showing the selected
value in the dropdown box.

So I want to use Javascript to get the selected query from the form.

I then want to pass it as a hidden field.

Why?
Planning to pass on this value in a series of pages?
If so, javascript is not the best solution. You may want to consider server-
side-scripting.

If you still want to do it this way, just have the hidden field already there
and use JS to fill in the value from the querystring.
http://www.ipwebdesign.net/kaelisSpace/useful_parseUrl.html

--
--
~kaeli~
She was engaged to a boyfriend with a wooden leg but broke
it off.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
C

cmc_dermo

Why? Because I need to and if i had to explain why then i'd totally
confuse people!

I looked at your example using Firefox and it didn't work.
 
R

RobG

I have a form that has a select list.

A user chooses a value and the page refreshes showing the selected
value in the dropdown box.

So I want to use Javascript to get the selected query from the form.

I then want to pass it as a hidden field.

So for example the section of the page I'm interested in will look like
this.

<form action="asubmitpage.htm" method="post">

<select name="ent" onchange="form.submit();">
<option value="UK">UK</option>
<option selected value="USA">USA</option>
<option value="JAPAN">JAPAN</option>
<option value="UK">UK</option>
</select>

I then want to take the selected value and submit it as a hidden field.

<form action="setpov.htm" method="post">
<input type="hidden" name="Entity" value=" this is where i want to fill
this with the selected value ">
</form>

I take it javascript is the easiest way to do this.

Your explanation is very confusing. As far as I can tell, you have two
forms. When an option is selected in one form, you want that form to
submit, then re-load the same page with the selected option as the
default selected.

e.g. if I selected "JAPAN", the form submits and the page loads with
"JAPAN" as the default selected, not "USA".

When the page loads, you want to copy the value of the selected option
to a hidden field in another form. Is that it? Seems unusual, but
you're obviously keen to do it.

The obvious solutions are:

1. Have one form and submit it all in one go, no need for the onchange
submit.

2. When the first form submits, return the selected value in the hidden
field. You say this isn't an option, yet you must be going to
change the default selected option anyway.

Given neither of the above seems appropriate:

3. Have the second form get the value of the selected option when it
submits. Then the first doesn't need to submit at all (submitting
onchange is nasty at any time - the user has to repost the form if
they make a simple slip-up).

4. Have an onload function that gets the value of the selected option
and puts it into the hidden field in the second form.
 
K

kaeli

Why? Because I need to and if i had to explain why then i'd totally
confuse people!

Explaining things often shows that you're going about things the hard way or
shows complete errors in logic. Which is why we ask.
I looked at your example using Firefox and it didn't work.

Please quote what you are replying to.
And it works in my firefox.
What version are you using and explain "doesn't work". Did you get an error?
It isn't supposed to DO anything except fill in the form with the same values
you already chose. Look at the URL to see that the form was submitted. It's
not fancy. It's a simple example and needs fleshed out with url encoding and
whatnot for production use.

--
 
T

Thomas 'PointedEars' Lahn

I have a form that has a select list.

A user chooses a value and the page refreshes showing the selected
value in the dropdown box.

So I want to use Javascript to get the selected query from the form.

I then want to pass it as a hidden field.

So for example the section of the page I'm interested in will look like
this.

<form action="asubmitpage.htm" method="post">

<select name="ent" onchange="form.submit();">
^^^^^^^^^^^^^^^^^^^^^^^^^
Don't. It is likely to make the form unusable without a pointing device
if client-side scripting is even supported. Users don't expect forms to
be submitted when they select a value of of its controls, they expect it
to be submitted when they activate a button control instead.
<option value="UK">UK</option>
<option selected value="USA">USA</option>
<option value="JAPAN">JAPAN</option>
<option value="UK">UK</option>
</select>

I then want to take the selected value and submit it as a hidden field.

<form action="setpov.htm" method="post">
<input type="hidden" name="Entity" value=" this is where i want to fill
this with the selected value ">
</form>

<meta http-equiv="Content-Script-Type" content="text/javascript">
...
<script type="text/javascript">
function setValue(oForm, index, value);
{
var e;
if (oForm
&& (e = oForm.elements)
&& (e = e[index])
&& typeof e.value != "undefined")
{
e.value = value;
}
}

function getSelectedValue(oSelect)
{
if (oSelect)
{
if (oSelect.selectedIndex >= 0)
{
return oSelect.options[oSelect.selectedIndex];
}
else
{
return ""; // or any dummy value
}
}
}
</script>
...
<form action="setpov.htm" method="post">
<div>
<input type="hidden" name="Entity" value="">
<select name="ent"
onchange="setValue(this.form, 'Entity',
getSelectedValue(this));">
[...]
</select>
<input type="submit" ...>
</div>
I take it javascript is the easiest way to do this.

*Client-side* JS is one way, however not the best one as it will not work
without client-side script support -- so you have to test server-side
anyway, why don't you just evaluate the value of the "ent" name-value pair
already submitted?


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top