I am having a small problem with getParameterValues function and my algoritm. Before I will explain how is my data table showed:
| value1 | value2 | TextArea | CheckBox |
Then when I check any checkbox and press submit it call other servlet and read the parameters :

String[] checkboxes= request.getParameterValues("checkbox");   
String[] textareas= request.getParameterValues("textarea");   
  
   for (int j = 0; j < checkboxes.length; j++) {   
                id = checkboxes[j];    
                variable = observacion[j];   
                boolean i = userdao.modificaSolicitud(id,  variable); // this is a update sql method   
                   
            }

The problem is that getParameterValues readthe checkbox checked (that is fine) but read all the textareas input (empty or not). Therefore I have 2 arrays diferents length.
| values | textarea1 | checkbox1 |
| values | textarea2 | checkbox2 |
| values | textarea3 | checkbox3 |
If check the checkboxes 1 and 3 and change the text in the textareas 1 and 3 respectively then when I execute the update it textareas values appear in the wrong place, is to say, textarea3 value will appear in textarea2.
I trying to relation both elements (checkbox and textarea). I can not to use the value property in the textarea for obvious reasons, also I can not to use the ID because getParameterValues not use it, then What I can to do?

Muchas Gracis for All.

Recommended Answers

All 5 Replies

I am really not sure if it will work, but I think that if an element is set to be disabled then it is not sent.
So you can try this:
Whenever you click a check box, use javascript to enable the text field attached to it. If you un-check it then disable the text field.

Try then to send and see what will be passes at the request.

If it doesn't work, then there is another way, less elegant. Let me know.

I am really not sure if it will work, but I think that if an element is set to be disabled then it is not sent.
So you can try this:
Whenever you click a check box, use javascript to enable the text field attached to it. If you un-check it then disable the text field.

Try then to send and see what will be passes at the request.

If it doesn't work, then there is another way, less elegant. Let me know.

thanks for you reply. I will try it today.
Yes you are right, if the element is disabled is ignored for getparametervalues.
I am thinking How to relation the element is javascript!, is to say, I will try to use SubString method for retrieve the last character in the name element for example:
textarea1 and checkbox1 if both end in "1" then unebled or disabled the element.
Thanks again, I will let know you later my conclusion

PD: Excume me for my bad english, is not my main languages

Hi JavaAddict
I am trying to get the solution and I think I have it but...I need to solve a small problem with javascript arrays. Appear some undefined values and I dont get how eliminate these.
here the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 
 </HEAD>

 <BODY>
 <FORM METHOD=POST ACTION="" name="solicitud" id="frm1">
	
 <TABLE>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text1"  ></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check1"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text2" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check2"></TD>
 </TR>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text3" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check3"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text4" ROWS="" COLS="" disabled='disabled'></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check4" ></TD>
 </TR>
 </TABLE>
 </FORM>

 
 <script type="text/javascript">

var x=document.getElementById("frm1");
var j=x.length;
var texto=document.getElementsByTagName("TEXTAREA");
var check=document.getElementsByTagName("INPUT");
var jhon=new Array();
var jhon1=new Array();

alert(texto.length+" "+check.length);

for (var i=0;i<j;i++)
  {
  if(x.elements[i].id=="check"){
	jhon[i]=x.elements[i].name;
  }else if(x.elements[i].id=="text"){
	jhon1[i]=x.elements[i].name;
  }
  }

for (var r=0;r<jhon.length;r++)
  {
	 document.write(jhon[r]+"  "+ jhon1[r]);
	 document.write("<br />");
  }
alert(jhon.length);
alert(jhon1.length);

</script>

 </BODY>
</HTML>

Is a simple example, please run it and see the result then you will understand better.

Muchas Gracias

If you use getParameterValues()
The name attribute of input tag and textarea tag must be same name

EX.

<input type="text" name="test" />
<input type="text" name="test" />
String val[] = request.getParameterValues("test")

I weak english language.

Hi JavaAddict
I am trying to get the solution and I think I have it but...I need to solve a small problem with javascript arrays. Appear some undefined values and I dont get how eliminate these.
here the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 
 </HEAD>

 <BODY>
 <FORM METHOD=POST ACTION="" name="solicitud" id="frm1">
	
 <TABLE>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text1"  ></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check1"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text2" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check2"></TD>
 </TR>
 <TR>
	<TD> <TEXTAREA id='text' NAME="text3" ROWS="" COLS="" disabled></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check3"></TD>
 </TR>
 <TR>
<TD> <TEXTAREA id='text' NAME="text4" ROWS="" COLS="" disabled='disabled'></TEXTAREA></TD>
	<TD> <INPUT id='check' TYPE="checkbox" NAME="check4" ></TD>
 </TR>
 </TABLE>
 </FORM>

 
 <script type="text/javascript">

var x=document.getElementById("frm1");
var j=x.length;
var texto=document.getElementsByTagName("TEXTAREA");
var check=document.getElementsByTagName("INPUT");
var jhon=new Array();
var jhon1=new Array();

alert(texto.length+" "+check.length);

for (var i=0;i<j;i++)
  {
  if(x.elements[i].id=="check"){
	jhon[i]=x.elements[i].name;
  }else if(x.elements[i].id=="text"){
	jhon1[i]=x.elements[i].name;
  }
  }

for (var r=0;r<jhon.length;r++)
  {
	 document.write(jhon[r]+"  "+ jhon1[r]);
	 document.write("<br />");
  }
alert(jhon.length);
alert(jhon1.length);

</script>

 </BODY>
</HTML>

Is a simple example, please run it and see the result then you will understand better.

Muchas Gracias

What are you trying to accomplish with that code. Because there is no need to take the form as an object: document.getElementById("frm1") and you can use the same method to get the Text Area.
Also check the API of those elements that you get and see what attributes they have:
http://www.w3schools.com/jsref/default.asp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.