Click to See Complete Forum and Search --> : Dynamically change variables in FORM without reload page


lu_norman
September 9th, 2006, 12:59 PM
Hi all!
How can i do this:

<form method="get" action="test.asp" >
<input name="boys" type=radio value="John">John
<input name="boys" type=radio value="Lu">Lu
<input name="boys" type=radio value="James">James
<input name="student" type=checkbox value="yes">student?

and now: if student=yes then this boy is a student:

<input type=hidden name="message1" value="he is a student">
<input type=hidden name="message2" value="welcome">

or if student not "yes" then boy is not a student:

<input type=hidden name="message1" value="who is it">
<input type=hidden name="message2" value="bye">

<input type="submit" value="Ок">
</form>

Thanks

wildfrog
September 9th, 2006, 02:55 PM
You can handle the forms onsubmit event and manipulate the data before it is submitted. Something like this:

<html>

<script language="Javascript">
function dostuff()
{
var student = document.getElementById("student");
var message1 = document.getElementById("message1");

// is student checked or not?
if (student.checked)
{
message1.value = "he is a student";
} else
{
message1.value = "who is it";
}
}
</script>


<body>
<form method="GET" onsubmit="js:dostuff()" action="test.asp">
<input name="boys" type=radio value="John">John
<input name="boys" type=radio value="Lu">Lu
<input name="boys" type=radio value="James">James
<input name="student" type=checkbox value="yes">student?

<input type=hidden name="message1" value="">
<input type=hidden name="message2" value="welcome">

<input type="submit" value="ОK">
</form>
</body>
</html>


- petter

lu_norman
September 9th, 2006, 04:09 PM
thanks a lot petter

PeejAvery
September 9th, 2006, 04:20 PM
Yes, onsubmit would work, but I do not think it is the best option. In this case you should use the onchange function with the actual HTML object.

<script language="JavaScript">
function chkstudent(which){
if(which){
document.getElementById("message1").value = "he is a student";
document.getElementById("message2").value = "welcome";
}
else{
document.getElementById("message1").value = "who is it?";
document.getElementById("message2").value = "bye";
}
}
</script>

<form method="get" action="test.asp" >
<input name="boys" type=radio value="John">John
<input name="boys" type=radio value="Lu">Lu
<input name="boys" type=radio value="James">James
<input name="student" type=checkbox value="yes" onchange="chkstudent(this.checked)">student?
<input type=hidden id="message1" value="-blank-">
<input type=hidden id="message2" value="-blank-">
<input type="submit" value="??">
</form>