Solved

how to get the parameter values, should  the parameter names is assigned danamically ?

Posted on 2006-11-29
28
220 Views
Last Modified: 2010-04-01
hello,

in a jsp page, there is a list of records retrieved from a database. and for each record i want to pass a ID value which is the primery key.

in jsp something like:
-------------------------------
<form >

<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....

<input type="hidden" name="memberIDNumber" value="<%=memberIDList.get(i).toString() %>" />
<a href="#" onClick="submitThisForm(3)">
    <img src="editable.gif" border="0" alt="Edit profile"></a>

<% } %>

</form>
------------------------------------

for each record, there is an unique ID value, whenv user click the "edit" image, the parameter "memberIDNumber"  will be submited to testID.java

my questions is:
the error i got is:  no matter i click which "edit" image of these printed records, the value of "memberIDNumber" i retrived from testID.java is always same

here, i'm using a same valriable name: "memberIDNumber"  in the input field.  is ithis the reason for the error? it seems it only pased the last element (ID value) in the memberIDList.

Thank you so much for your reply,

lilyyan


0
Comment
Question by:lilyyan
  • 15
  • 13
28 Comments
 

Author Comment

by:lilyyan
ID: 18038370
hello again,

well, this question is actually is an extend of my another question in jsp: "sumbit forms through a javascript and assign each form a different name dynamically".

in that question, the solution is: i used only one form for all submit actions. based on the error in the current posting, i guess i still need to use a different form for each submit action.

or is there another way to solve the error?

thanks so much for your reply,

lilyyan
0
 
LVL 12

Accepted Solution

by:
enachemc earned 400 total points
ID: 18038617
<form >
<input type="hidden" name="memberIDNumber" id="memberIDNumber" />

<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....

<a href="#" onClick="document.getElementById('memberIDNumber').value=<%=memberIDList.get(i).toString() %> ;submitThisForm(3)">
    <img src="editable.gif" border="0" alt="Edit profile"></a>

<% } %>

</form>
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18038626
you were actualy submitting one hidden value for each row. In the response servlet you probably retrieved only the first value (getParameterValue).
0
 

Author Comment

by:lilyyan
ID: 18039391
hi enachemc,

thanks so much! this works great : D

only one question, why the response servlet only retrieve the first value?

lilyyan
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18039576
because that is what you request. If you want all the values, request them all: getParameterValues(paramName)
0
 

Author Comment

by:lilyyan
ID: 18039746
q1:

getParameterValues(paramName) is used for retrieving parameters in an array, so if getParameterValues(paramName) in the response servlet, all the value in memberIDList.size() will be retrieved?

===========================
i once used something like:

while(sqlRst.next()) {

out.println("<tr><td>Core No. </td>");
out.println("<td>File Name</td>");
out.println("<td>Version No.</td>");
.....
out.println("<a href=\"./coreStandardsF/" + fileName+ "\""+ " target=_blank>" +
fileName+ "</a>" +"<form action=\"delteCoreFiles.jsp\"  method = \"post\" name=\"deleteFilesForm\" onsubmit=\"return disp_confirm(this)\">"+

"<input type=\"hidden\"  name=\"filesName\"  value=\""+fileName+ "\"/>"+
"<input type=\"submit\" value=\"Delete this file?\" />"+"</form></td><td>");

} // end of while

===================

q2:

in the above code, i used different form for each record, and they all have the same name: "deleteFilesForm"  
and the coding is working.

my questions is , why in this case the forms having same name, but it's working?

and in the question of this posting i have to use only one form for all the submit actions?



0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 400 total points
ID: 18039802
yes, it's because of the form(s). Only one form in a webpage is submitted to the server. The other ones are ignored. In your first post, you had one form with more fields with the same name - this would result in more values for a parameter; in your last post here you had multiple forms, each containing one field - this results in sending only the field in the submitted form getting to the server. The others, as I said, are ignored.
0
 

Author Comment

by:lilyyan
ID: 18039973

will this work?

---------------------------

<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....
<form method="post" action=""  name="editForm" >
<input type="hidden" name="memberIDNumber" value="<%=memberIDList.get(i).toString() %>" />
<a href="#" onClick="submitThisForm(3)">
    <img src="editable.gif" border="0" alt="Edit profile"></a>
</form>
<% } %>

-----------------------------------------------------

well, actually it's not working. but i'm not sure the reason.
is that beacuse i'm not uing submit button,  instead i'm using a href link to submit the form?

0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040022
yes and no.
yes .... but
the submitThisForm would have to know what form to submit
0
 

Author Comment

by:lilyyan
ID: 18040248
the form name is "editForm", the form id is: editDeleteFormID

is javascript of submitThisForm

document.getElementById("editDeleteFormID").submit();

the only difference here is: one is using a submit button and the other is using a href link

but why the one using a href link is not working ?
0
 

Author Comment

by:lilyyan
ID: 18040301
also in:
----------------------------------------
<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....
<form method="post" action=""  name="editForm" >
<input type="hidden" name="memberIDNumber" value="<%=memberIDList.get(i).toString() %>" />
<a href="#" onClick="submitThisForm(3)">
    <img src="editable.gif" border="0" alt="Edit profile"></a>
</form>
<% } %>

------------------------------------------
if i change to:

<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....
<form method="post" action=""  name="editForm" >
<input type="hidden" name="memberIDNumber" value="<%=memberIDList.get(i).toString() %>" />
< input type="image" src="editable.gif" border="0" alt="Edit profile" value ="Edit">
</form>
<% } %>

----------------------------------------------------------

this is work, right?

0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040304
because a submit button is inherently associated with the parent form and will submit the parent form. A function is not part of a form. You will need a way to distinguish the forms.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040352
you can transmit a reference of 'this' to the function from an input of the form to the submitThisForm(3) (like submitThisForm(3, this) ) and in the function, submitThisForm(no, inputEl) use inputEl.form.submit().
0
 

Author Comment

by:lilyyan
ID: 18040427
submitThisForm(3, this) : "this" is referred to the form, right?

inputEl.form.submit(). : inputEl is a variable name which reference to "this",

but why you use another form here: i..e nputEl.form.submit()   // .form.

will this work: document.inputEl.submit()  ?

also would you please reply my last posting ?

thanks so much
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:enachemc
ID: 18040447
this is a reference to the input, not the form.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040456
it will not work. There is no one to submit the form.
0
 

Author Comment

by:lilyyan
ID: 18040528
if i change to:

<% for (int i=0; i<memberIDList.size(); i++){ %>

code for printing record (i) ....
<form method="post" action="edit.jsp"  name="editForm" >
<input type="hidden" name="memberIDNumber" value="<%=memberIDList.get(i).toString() %>" />
< input type="image" src="editable.gif" border="0" alt="Edit profile" value ="Edit">
</form>
<% } %>

---------------------------------------

the image button is a submit button
0
 

Author Comment

by:lilyyan
ID: 18040558
>>this is a reference to the input, not the form.

so in inputEl.form.submit(), inputEl.form is actually reference the input field of the form, right?

i don't know an element of a form can be accessed in this way,  may you post some tutorial link about this, if possible?

0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040571
yes, you're right. And yes, it will work. If this is the entire form, you will have a single parameter named memberIDNumber. Actualy you will also have two parameters named x and y generated by the image input. You can see this in the URL if you change the method of the form from post to get.
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 400 total points
ID: 18040585
no, inputEl is a reference to the input (since it comes that way - it is the input that generate the event and calls the function submitThisForm(3, this) ; Here this is the element. then inputEl.form equals the form. Now that you have a reference to the form, you can submit it.
0
 

Author Comment

by:lilyyan
ID: 18040717
so, for example: in the case of submitting a form within a for/while loop and also the submit action is not fired by a real submit button,

 i can submit a form through a field reference and plus form, namely the format is :


fieldReference.form  // this will reference to the form, is this right?
0
 

Author Comment

by:lilyyan
ID: 18040729
i'm not quite get use to this notiation: fieldReference.form,
is there any tutorial link for this?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040760
0
 

Author Comment

by:lilyyan
ID: 18040923
well , i try to find the input has a property of type form from the link : http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/input_checkbox.asp

but i didn't find it, would you please paste the code in that link ?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18040960
0
 

Author Comment

by:lilyyan
ID: 18041007
this is code in the link: i didn't see any code indicate that input has a property type of form

--------------------------------------------------------------------------------------------------------


<INPUT TYPE=checkbox CHECKED ID=chk1 onclick="choosebox1()">Uncheck
    this check box for some free advice.
<P><INPUT TYPE=checkbox ID=chk2 onclick="choosebox2()">Or check
    this check box for a message from our sponsors.
<P ID=SampText>

This example implements the script when the user clicks either check box.

<SCRIPT>
function choosebox1(){
            alert("Never play leapfrog with a unicorn!")
}
function choosebox2(){
      SampText.insertAdjacentHTML("AfterBegin","Buy WonderWidgets! ");
}
</SCRIPT>

-----------------------------------------------------------------------------------
0
 

Author Comment

by:lilyyan
ID: 18041141
really appreciate your replies. these replies are very helpful, sorry if i asked to many questions: D
0
 

Author Comment

by:lilyyan
ID: 18041189
3 A there :D
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

A safe way to clean winsxs folder from your windows server 2008 R2 editions
We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now