0

Hi, I'm starting to learn Java EE - JSP / SERVLET and as for my learning progress, I'm trying to achieve this kind of web app but Im having problem with displaying the right selected row (TASK ID Column) from my table. It is populated from my oracle db. Im able to display my expected output correctly. But when I'm trying to get the selected row through clicking the corresponding button, I'm always getting the 1st row value, i.e. ID = '123456', no matter what Button I clicked, 2nd, 3rd, etc. It always display the 1st row. You can kindly see the image below. Hoping someone can guide me to my problem. Thanks.

Note: Apologies for my coding conventions and use of scriptlet, I may later use JSTL /EL after I got used to the basics of JAVA EE. Thanks.

EXPECTED OUTPUT:
7e4dda1aa1a2f58819a96efca9ea0a45

To check if its getting the correct value, I used javascript to retrieve the value of TaskID.

PARTIAL JSP CODE: I've cut some part of codes, displaying only TASKID and APPROVE Button for checking value purposes.

`<%
String strQuery = "SELECT * FROM SIM_TEST";

            try{
                Connection conn = ConnectionProvider.getConn();
                PreparedStatement ps = conn.prepareStatement(strQuery);
                ResultSet rs = ps.executeQuery() ;

                int status;
                String getId;


                    while (rs.next())
                    {
                      String taskId = rs.getString("TASKID");
              %>      
                      <tr>
                      <td><%=taskId%></td>

                      <td><input type="button" id="btnApprove" value="Approve" />
                          <input type="hidden" id="getTaskId" onclick="getTaskId();" value="<%=taskId%>" />
                      </td>
                      </tr>
              <%

                    }

                }catch(Exception e){
                    e.printStackTrace();
                }
            %>`

And to check if its getting the value of hidden type when APPROVE button is clicked, I use javascript.

<script type="text/javascript">
    function getTaskId(){
        var id = document.getElementById('getTaskId').value;
        alert(id);
    }
</script>
3
Contributors
6
Replies
57
Views
4 Years
Discussion Span
Last Post by Om_3
0

Correction for button action and hidden field value.

<td><input type="button" id="btnApprove" onclick="getTaskId();" value="Approve" />
<input type="hidden" id="getTaskId"  value="<%=taskId%>" />
</td>

Still cant get the specific row value from the table. Always prompting "123456". Any tips to overcome this?

0

hai batuzai04123 ,

why dont we try to pass taskid value to java script from button onclick eventt like as follows

<input type="button" id="btnApprove" onclick="getTaskId(<%=taskId%>);" value="Approve" />

if you try like as above there is no need of hidden feild as well as you may get the task id directly in java script also

try like this and let me know if you have any doubts in my clarification

happy coding

Edited by radhakrishna.p: giving clarity on the solution

0

a big thanks radhakrishna, that code really work, but is there other way I could get the value using jquery, I cant seem to get the value of function gettaskid() inside the $(document).ready(function). Also, I realize its not really the way I should write javascript inside html tags, am I right? Tried already using jquery to get the taskid but it displays only the first row, but only your solution works! and Im lost at this point.

Thanks.

Edited by batuzai04123

0

hai batuzai04123,

its so easy to get the same kind of thing using jquery for this you have to do the following things in your code

so let be your code like as follows (as previous)

<td>
<input type="button" id="<%=taskId%>" class="approve" value="Approve" />
<input type="hidden" id="Task<%=taskId%>" value="<%=taskId%>" />
</td>

then inside jquery place the following code

$(".approve").click(function(){
        var selecred_btn_id = $(this).attr("id");

        var sel_task_id = $("Task"+selecred_btn_id).attr("id");

        window.alert(sel_task_id);      
        // your remaing code goes here

})

you will get the approve button selected task id as you wish

i think you the point

check it once by doing as i explained above

let me know the status

note: i apologies to the members over here because i posted the code than explaining ( i feel beter by giving the code that explaining the concept)

Edited by radhakrishna.p: giving clarity on thesolution

0

hai members , sorry for my gramitical mistakes in some sentences of above post

Edited by radhakrishna.p: giving clarity on the solution

0

I am facing same issue. ...this solution is not working

Please help...... when button is clicked from row.. each button is giving same id =1...

Edited by Om_3

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.