Shashi Kiran
Shashi Kiran

Reputation: 362

To pass a parameter form one jsp page to another using Onclick

I need to pass the id parameter from task.jsp to edit.jsp.

I tried using the set and get parameter with input type as hidden, but was unable to get the id in the edit.jsp

This is a part of my task.jsp page

<tr>
                <%-- <td><%=currentTask.getId()%></td> --%>
                <td><%=currentTask.getTaskDescription()%></td>
                <td><%=currentTask.getDate()%></td>
                <td><%=currentTask.getUser().getUserName()%></td>
                <td><input id="<%=currentTask.getId()%>" type="button"
                    onclick="editAction(this)" value="Edit" /></td>
                <td><input id="<%=currentTask.getId()%>" type="button"
                    onclick="deleteAction(this)" value="Delete" /></td>
            </tr>
            <%
                }
            %>
        </table>
    </div>
    <p />
    <input type="button"
        onclick="window.location='/HibernateWebApp/login.jsp';"
        value="Logout">

    <%
        System.out.println(userName);
    %>
</form>
<b><a href="CreateTask.jsp?userName=${userName}"> Click here
        to add a new task </a></b>


<script>
    function editAction(item) {
        int id = item.id;
</script>
<input type="hidden" name="id" id="id" />
<script>
    document.getElementById("id").value = id;
        document.form.action = "edit.jsp";
        document.form.submit();
        console.log(item.id);
    }
</script>


<script>
    function deleteAction(item) {
        console.log(item.id);
    }
</script>

This is the error I get in my console when I click on the edit button - ReferenceError: editAction is not defined

How do I pass the parameter from task.jsp and get it is edit.jsp

Upvotes: 0

Views: 5401

Answers (2)

camcam
camcam

Reputation: 1

I had this problem too and I found this way to do :

In the html :

<button><a href="ServletName?id=${UserName}">ButtonName</a></button>

In the servlet :

request.getParameter("id");

Upvotes: 0

Vishal Gajera
Vishal Gajera

Reputation: 4207

do something likewise either way...

task.jsp

<a href="t1.jsp?val=<%=currentTask.getId()%>">Edit</a>

    <!-- OR -->

    <form action="edit.jsp" method="post">
        <input id="id_anything123" type="hidden" name="val" value="<%=currentTask.getId()%>" />
        <input id="<%=currentTask.getId()%>" type="submit" value="Edit" />
    </form>

edit.jsp

 <%= request.getParameter("val") %> <!-- will print value which you have pass from task.jsp -->

Upvotes: 2

Related Questions