Poojith Jain Poojith Jain - 13 days ago 10
HTTP Question

Do hidden fields always submit the first value from a list of items?

I've been trying to solve this for a long time. Not sure where I'm going wrong.
I display a list of websites from the database, where each of these URLs have unique IDs (I'm saving these as hidden fields).

for(int i = 0; i < favoriteList.length; i++) {

String url = favoriteList[i].getUrl();
out.println("<tr>");
out.println("<td>");

out.println(" <form id=\"countForm\" method=\"POST\">");
out.println(" <input type=\"hidden\" name=\"favoriteID\" value=\""
+ favoriteList[i].getFavoriteId() + "\" />");
out.println("<a"
+ " href =\" javascript:document.countForm.submit();\""
+ " onclick=\"document.getElementById('countForm').submit();\"" + " />" + url + "</a>");
out.println("</form>");

out.println("</td> <tr> <td>");
out.println("<p>" + favoriteList[i].getComment() + "</p>" );
out.println("</td></tr> <tr><td>");
out.println("<p>" + favoriteList[i].getClickCount() + "</p>");
out.println("</td></tr>");
}


The list of websites are displayed correctly. However, after clicking a URL, the form should get submitted and the URL's count should get incremented (basically, trying to count the number of clicks). Whenever I try fetching the ID of the URL(from the hidden field) as
String updateAction = request.getParameter("favoriteID");
, the first link's ID (1) is getting read. Not sure if I'm doing it right.

EDIT:
I understand that writing Servlets is a bad idea, but this is for a homework, where I've got to use Servlets, instead of JSP or other alternatives.

Answer

If your favoriteList has more than one element, then you're creating multiple <form> elements, all named countForm.

document.getElementById('countForm') will return the first1 form by that name, so you're always posting the first form, not the form with the link in it.

One way to fix it, is to name the forms differently, e.g. suffix the form name with the value of i:

for(int i = 0; i < favoriteList.length; i++) {  

    String url = favoriteList[i].getUrl();
    out.println("<tr>");
    out.println("<td>");

    out.println(" <form id=\"countForm" + i + "\"  method=\"POST\">");
    out.println(" <input type=\"hidden\" name=\"favoriteID\" value=\""
                    + favoriteList[i].getFavoriteId() + "\" />");
    out.println("<a" 
        + " href =\" javascript:document.countForm" + i + ".submit();\""
    + "  onclick=\"document.getElementById('countForm" + i + "').submit();\"" + " />" + url + "</a>");       
    out.println("</form>");

    out.println("</td> <tr> <td>");
    out.println("<p>" + favoriteList[i].getComment() + "</p>" );
    out.println("</td></tr> <tr><td>");
    out.println("<p>" + favoriteList[i].getClickCount() + "</p>");
    out.println("</td></tr>");
}

1 Note: The fact that you're getting the first form is browser dependent. The DOM spec says:

Returns the Element whose id is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this id.