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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficult form problem tell me is it impossible

Status
Not open for further replies.

AnthonyGeorge

Technical User
Jun 3, 2004
46
GB
I have a difficult problem with no idea how to solve it.

The problem is as follows at the moment I have the following code

while(rs.next())
{
%> <form name="setlimits" method=POST action="controlservlet">
<tr>
<td>
<input type="text" name="new_limit" size=10 value="0" >
<input type="hidden" name="index" value="<%= rs.getString("index") %>">
<td>
<tr>
<tr>
<input type="submit" value="Amend Limit">
</form>
<tr>
<%
}

When I press submit the control servlet is called and two parameters passed to it new_limit and a corresponding index.

index = request.getParameter(“index”);
new_limit = request.getParmeter(“new_limit”);

This allows me to update a table row with the new_limit in the database where index = index.

What I have now been asked to do is supply a submit all button, when this button is pressed I should pass to the control servlet all the new limits with all the corresponding indexs.

Is this possible, I can not see any examples of code I could copy.

Thanks Tony

 
Not much is impossible ...

Here is one way how you may go around it :

Code:
<form name="setlimits" method=POST action="controlservlet">
<%
int id = 0;
while(rs.next())
{
%>
        <tr>
            <td>
                <input type="text" name="new_limit<%=id%>" size=10 value="0" > 
            </td>
            <td>
                <input type="hidden" name="index<%=id%>" value="<%= rs.getString("index") %>">
            </td>
        </tr>
 
<%
	id++;
}
%>
        <tr>
            <td>
      		<input type="submit" value="Amend Limit">
            </td>
        </tr>      		
</form>

and in your servlet, retrieve all the indexes, and do a bulk SQL update of all values :

Code:
int id = 0;
String index = "";
String newLimit = "";

while (index != null && index.length() != 0) {
	newLimit = request.getParameter("new_limit" +id);
	index = request.getParameter("index" +id);
	System.out.println("Index=" +index +", Limit=" +newLimit);
	id++;
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi sedj

The problem is they want both buttons i.e

index [100] limit [20] Amend limit
index [101] limit [25] Amend limit
index [102] limit [60] Amend limit
index [103] limit [80] Amend limit
index [104] limit [90] Amend limit

Amend All

pressing the Amend All button will extract all indexes and limits and submit them to the servlet.

Thanks foy your awnser though

Tony
 
You just need to think about the problem a little bit, and apply some javascript :

save this as "test_input.jsp"
Code:
<html>
<head>
<script language="javascript">
  var indexIds = "";
  function addId(index, id) {
	indexIds += (index +'|' + document.getElementById('new_limit'+id).value  +',');
	document.forms[0].indexIds.value = indexIds;
	document.getElementById('new_limit_cb'+id).checked = true;
	
  }
  
  function addAll() {
  	var id = 0;
  	indexIds = "";
  	while (true) {
  	  var idElement = document.getElementById('new_limit'+id);
  	  if (idElement) {
  	  	indexIds += (document.getElementById('new_limit_index'+id).value +'|' + document.getElementById('new_limit'+id).value  +',');
  	  	document.getElementById('new_limit_cb'+id).checked = true;
  	  	id++;
  	  } else {
  	  	break;
  	  }
  	}
  	document.forms[0].indexIds.value = indexIds;
  }
</script>
</head>
<body>
<table>
	<tr>
		<td>New Limit</td><td>&nbsp;</td><td>Change Limit ?</td>
	</tr>
	<tr>
		<td colspan="3"><hr/></td>
	</tr>
<%
int id = 0;
java.util.ArrayList al = new java.util.ArrayList();
al.add("abc");
al.add("def");
al.add("ghi");
for (int i = 0; i < al.size(); i++) {
String index = (String)al.get(i);
%>

        <tr>
            <td>
                <input type="text" id="new_limit<%=id%>" size=10 value="0" > 
                <input type="hidden" id="new_limit_index<%=id%>" value="<%= index %>" > 
            </td>
            <td>
              <input type="submit" onclick="addId('<%= index %>', '<%= id %>');" value="Add Limit Amendment">
            </td>
            <td>
                <input type="checkbox" id="new_limit_cb<%=id%>"> 
            </td>            
        </tr>
        
 
<%
    id++;
}
%>
        <tr>
            <td colspan="3">
                <input type="button" onclick="addAll();" value="Add all to amend" > 
            </td>
        </tr>    
</table>   
<p>
<form name="setlimits" method=POST action="test_go.jsp">
              <input type="submit" value="Submit changes">
              <input type="hidden" name="indexIds"> 
     
</form>
</body>
</html>

and this as "test_go.jsp" :

Code:
<%@ page import="java.util.*" %>
<%

String indexIds = request.getParameter("indexIds");
StringTokenizer idIndexParts = new StringTokenizer(indexIds, ",");
while (idIndexParts.hasMoreTokens()) {
	StringTokenizer st = new StringTokenizer(idIndexParts.nextToken(), "|");
	String index = st.nextToken();
	String id = st.nextToken();
	out.println("index=" +index +", new_limit=" +id +"<br>");
}



%>



--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top