Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transfering data between form tags

Status
Not open for further replies.

dexter195

Programmer
Jun 18, 2003
220
EU
hi

is there any way to pass the value of a selected radio button from one form tag to another.

i have the info thats being selected, which is taken from a vector using a loop. at the moment its only the last value in the loop thats getting passed to the other forms.

Code:
<FORM name="frmAdminModify" method="post" action="ModifyAdmin.jsp">
<INPUT NAME="designerUsername" TYPE="hidden" VALUE="<%= adminUsername%>">
<TD><INPUT NAME="cmdModifyAdmin" TYPE="submit" VALUE="Modify"></TD>
						</FORM>

adminUsername is the value of the whats behind the radio button. both these forms are on the same page. ive tried a couple of things with no success, each time only getting the last username in the vector.

i presume this is beacuse once the code is compiled on the server the last value of adminUsername is going to be stored and that what the next form is going to get. is there a way for the value of the selected username to be passed to the other form during runtime?

thanks for your help
 
<!--r1.htm-->
<html>
<head>
</head>
<body>
<form action="r2.jsp">
<input type="radio" name="ra1" value="ra1val">
<input type="radio" name="ra1" value="ra2val">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

<!--r2.jsp-->
<html>
<head>
</head>
<body>
<form>
<%
String result = request.getParameter("ra1");
out.println(result);
%>
<input type="radio" name="rb1" value="rr1" <%if (result!=null && result.equals("ra1val")) {%>checked<%}%>>
<input type="radio" name="rb2" value="rr2" <%if (result!=null && result.equals("ra2val")) {%>checked<%}%>>
</form>
</body>
</html>
 
hi prosper,

thanks for the responce.
sorry i forgot to put a few details in the original post.

what i ment to say was that i have a few forms on the one jsp page, and i was wondering if its possible to pass values from one to the other.

heres what im trying to do (im using hibernate to read from the database so never mind all that iterator stuff)
Code:
         <FORM name="frmAdminList" method="post">
	   <% for ( Iterator iter = lstAdmin.iterator(); iter.hasNext(); )
		  {
		  		// creating the administrator object
				adminOutput = (Administrator) iter.next();
			    // puts a checked radio button first
			    if ( counter == 0 )
				{
%>						<TD><INPUT CHECKED TYPE="radio" NAME="adminUsername" VALUE="<%=adminOutput.getUsername()%>"></TD>
<%					 	counter = 1; %>
	<%			}
				
				// otherwise just put an unchecked radio button in the table
				else
				{
					%> <TD><INPUT TYPE="radio" NAME="adminUsername" VALUE="<%=adminOutput.getUsername()%>"></TD>  <%
				}
		 }
	   %>
	   </FORM>
	   
	   <FORM name="frmAdminModify" method="post" action="ModifyAdmin.jsp">
         	<% String strAdminUsername = request.getParameter("adminUsername"); %>
			<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
			<TD><INPUT NAME="cmdModify" TYPE="submit" VALUE="Modify"></TD>
		</FORM>
		
		<FORM name="frmAdminDelete" method="post" action="DeleteAdmin.jsp">
            <% String strAdminUsername = request.getParameter("adminUsername"); %>
			<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
			<TD><INPUT NAME="cmdDelete" TYPE="submit" VALUE="Delete"></TD>
		</FORM>
		
		<FORM name="frmAdminDetails" method="post" action="AdminDetails.jsp">
            <% String strAdminUsername = request.getParameter("adminUsername"); %>
			<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
			<TD><INPUT NAME="cmdDetails" TYPE="submit" VALUE="Details"></TD>
		</FORM>

ive tried having the 3 button forms within the form with the radio tags. but this just gives errors when the other page opens up.

when i get the source after this page loads up all getParameter values are null.

is this even possible to do on the same page?

thanks for your help
 
i changed
Code:
<FORM name="frmAdminList" method="post">

to

Code:
<FORM name="frmAdminList" method="get" >

i put a submit button on the form so when you click it, it remembers the administrator you picked. then when i click one of the 3 buttons it will go to its page with the right info.

is there a way to automate the submit button being pressed when the radio button is updated?
 
<%@page import="java.util.*" %>
<%@page import="test.Administrator" %>

<html>
<head>
<script lang="JavaScript">
function f1()
{
var choice = 0, i = 0;
for (i=0; i<2; i++)
if (document.frmAdminList.adminUsername.checked)
choice = i;
document.frmAdminModify.tempAdmin.value=document.frmAdminList.adminUsername[choice].value;
document.frmAdminModify.submit();
}
function f2()
{
var choice = 0, i = 0;
for (i=0; i<2; i++)
if (document.frmAdminList.adminUsername.checked)
choice = i;
document.frmAdminDelete.tempAdmin.value=document.frmAdminList.adminUsername[choice].value;
document.frmAdminDelete.submit();
}
function f3()
{
var choice = 0, i = 0;
for (i=0; i<2; i++)
if (document.frmAdminList.adminUsername.checked)
choice = i;
document.frmAdminDetails.tempAdmin.value=document.frmAdminList.adminUsername[choice].value;
alert(document.frmAdminDetails.tempAdmin.value);
document.frmAdminDetails.submit();
}

</script>
</head>
<body>
<%
Vector lstAdmin = new Vector();
Administrator adminOutput = new Administrator("john lash");
out.println(adminOutput.getUsername());
lstAdmin.add(adminOutput);
adminOutput = new Administrator("Tom cruise");
lstAdmin.add(adminOutput);
int counter = 0;
%>
<FORM name="frmAdminList" method="post">
<% for ( Iterator iter = lstAdmin.iterator(); iter.hasNext(); )
{
// creating the test.Administrator object
adminOutput = (test.Administrator) iter.next();
// puts a checked radio button first
if ( counter == 0 )
{
%> <TD><INPUT CHECKED TYPE="radio" NAME="adminUsername" VALUE="<%=adminOutput.getUsername()%>"></TD>
<% counter = 1; %>
<% }

// otherwise just put an unchecked radio button in the table
else
{
%> <TD><INPUT TYPE="radio" NAME="adminUsername" VALUE="<%=adminOutput.getUsername()%>"></TD> <%
}
}
%>
</FORM>
<FORM name="frmAdminModify" method="post" action="ModifyAdmin.jsp">
<%
String strAdminUsername = request.getParameter("adminUsername"); %>
<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
<TD><INPUT NAME="cmdModify" TYPE="button" VALUE="Modify" onClick="f1()"></TD>
<INPUT TYPE="HIDDEN" NAME="tempAdmin" value="">
</FORM>

<FORM name="frmAdminDelete" method="post" action="DeleteAdmin.jsp">
<%strAdminUsername = request.getParameter("adminUsername"); %>
<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
<TD><INPUT NAME="cmdDelete" TYPE="button" VALUE="Delete" onClick="f2()"></TD>
<INPUT TYPE="HIDDEN" NAME="tempAdmin" value="">
</FORM>

<FORM name="frmAdminDetails" method="post" action="AdminDetails.jsp">
<% strAdminUsername = request.getParameter("adminUsername"); %>
<INPUT NAME="adminUsername" TYPE="hidden" VALUE="<%= strAdminUsername %>">
<TD><INPUT NAME="cmdDetails" TYPE="button" VALUE="Details" onClick="f3()"></TD>
<INPUT TYPE="HIDDEN" NAME="tempAdmin" value="">
</FORM>
</body>
</html>
 
cheers for your help prosper,

i had to perform the action differently because i had to create different forms dynamically depending on what the user had permission to do with the administrator.

this is how i did it.

thanks again for your help

Code:
<SCRIPT language="JavaScript">

	function setChosenAdmin()
	{
		var radiobuttons = document.forms['frmAdminList'].elements['adminUsername'];
						
		// formsArray will store the values of the adminmod for the form at i
		var formsArray = new Array();
		
		// this loop will store the form info into the adminmod array
		for ( var i = 0; i < document.forms.length; i++ )
		{					
			var adminmod = document.forms[i].elements; // stores the elements of this form into the adminmod array
			formsArray[i] = adminmod; // stores this array into the formsArray
		}
		
		// loops through the radio buttons to see what radio button was clicked
		// and then adds the value of the radio button to adminUsername in all the forms.
		for ( var ri = 0; ri < radiobuttons.length; ri++ )
		{
			  // checks to see what radio button was checked
			  if ( radiobuttons[ri].checked )
			  {
					// this loop will store the value of the selected radio button in all of the forms
					// this starts at 1 beacause the first form isn't required for sending data to other forms.
					for ( var j = 1; j < formsArray.length; j++ )
					{
						// stores the value of the form at position i
						var temp = formsArray[j];
						// this is then let equal to the value of the radio button that was selected
						temp['adminUsername'].value = radiobuttons[ri].value;
						
					} // end nested loop that passes the value of the radio button to all the forms.
				
					break; // stops the loop when the checked button is found
					
			  } // end if to see what radio button was checked 
			
		}// end for
		
	}	// end function setChoseAdmin()
	
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top