Search code examples
javajspenterprise

How to persist another table from a resultList singly


I have been trying this for one week now. I have a working method in my session bean that works very fine and display list of users from MySQL table.

I want to select a particular user using a radio input type and insert his details into another table.

My problem is that each time I try, it is the first value of the data displayed that enters the table instead of the data for which I clicked its id on the radio input type. I dont know what am getting wrong.

    <%

    if (request.getParameter("submit) != null 
     && request.getParameter("id") != null){              

        String id   = request.getParameter("id");             
        String vote = request.getParameter("vote");
        String name = request.getParameter("name");
        String code = request.getParameter("code");
        String msg  = "";

        try {
            Result one = new Result(id, vote, name, code);
            sl.castvote(one);
            msg = "User successfuly added";
        } catch (Exception e) {
            msg ="error";
        }
    }

    %>
      <% List one = sl.viewAllContestants();
         Iterator two = one.iterator();
      <%
         while(two.hasNext()) {
             Contestants type = (Contestants) two.next();
       %>

         <form action="" method="post">

         <input type="radio" name="id" class="form-control" value="<%=type.getId()%>"  />
         <input type="text"  name="vote" value="1" hidden />
         <input type="text"  name="dept" value="<%=type.getDepartment() %>" hidden />
         <input type="text"  name="col"  value="<%=type.getCollege() %>" hidden /> 
         <input type="text"  name="pos"  value="<%=type.getPosition() %>" hidden />
         <input type="text"  name="type" value="<%=type.getElectionType()%>" hidden />
         <input type="text"  name="name" value="<%=type.getName() %>" hidden  />
         <input type="text"  name="code" value="<%=type.getCode() %>" hidden />
         <input type="submit" name="submit" />

         </form>

         <% } %>    

Solution

  • It's a bit hard to read that scriptlet and I might be wrong but what I see here is that your result page has something like:

    <form>
      <radio name="id" value="some value" />
      some other fields
      <submit>
    </form>
    

    Then another form:

    <form>
      <radio name="id" value="some value" />
      some other fields
      <submit>
    </form>
    

    And yet another for the next one in the iterator. That basically is not a proper way to do it. First of all you have the different values of the radio buttons in different forms. So it won't really matter which one is selected :) Also the forms don't have different names which actually breaks the way html works.

    So in order to make that work (I believe it will work even though its not a good idea) you can give different names to the different forms. So they become something like <form action="" method="post" name="firstForm"> etc. In this you can make the submit to submit only the form where it is by changing it to:

    <button type="submit" form="firstForm" value="Submit">Submit</button>
    

    Because with your current design all your submit buttons submit the first form ;)

    • To give you some food for thoughts maybe the proper way to do it is to have one form in the page instead of multiple ones. But in your scenario it might require some javascript or redesign of the page.