Arshad Ali
Arshad Ali

Reputation: 3274

how to sent data by clicking a link in jsp

I want to get data from db and display in a table and each record should have a link for See Details , for this I have :

<form name="submitForm" method="POST" action="details.jsp">
    <table border="1" width="80%">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>More</th>
        </tr>

        <%
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                connection = com.secure.db.DatabaseManager.getConnection();
                statement = connection.createStatement();
                resultSet = statement.executeQuery("SELECT * from address");
                while (resultSet.next()) {
        %>
        <tr>
            <td><%=resultSet.getString("first_name")%> <input type="hidden"
                name="fname" value="<%=resultSet.getString("first_name")%>"></td>
            <td><%=resultSet.getString("last_name")%> <input type="hidden"
                name="lname" value="<%=resultSet.getString("last_name")%>"></td>
            <td>    <a href="javascript:document.submitForm.submit()">See Details</a></td>
        </tr>
        <%
            }
        %>

    </table>

    <%
    } catch (Exception ex) {
        ex.printStackTrace();
%>
    Ooops, something bad happened:
    <%
    } finally {
        if (resultSet != null)
            resultSet.close();
        if (statement != null)
            statement.close();
        if (connection != null)
            connection.close();
    }
%>
</form>

Now result is :

enter image description here

when I click on first record I get correct information, but when I click on other than first record I'm still getting data of first record. how to get correct data?

Upvotes: 0

Views: 3914

Answers (2)

Rakesh Chouhan
Rakesh Chouhan

Reputation: 1230

try this code

        <table border="1" width="80%">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
<th>&nbsp;</th>
            </tr>

            <%
                Connection connection = null;
                Statement statement = null;
                ResultSet resultSet = null;
                try {
                    connection = com.secure.db.DatabaseManager.getConnection();
                    statement = connection.createStatement();
                    resultSet = statement.executeQuery("SELECT * from person");
                    int iCount=0;


 while (resultSet.next()) {
            iCount =iCount+1;
                %>
                <tr>
                    <td colspan="3">
     <form name='submitForm_<%=iCount%>' method="POST" action="details.jsp">
    <table><tbody><tr><td>

    <input type="hidden" name="first_name"
                        value='<%=resultSet.getString("first_name")%>'><%=resultSet.getString("first_name")%></td>
                    <td><input type=="hidden" name="last_name"
                        value='<%=resultSet.getString("last_name")%>'><%=resultSet.getString("last_name")%></td>
                   <td><a href="javascript:document.submitForm_<%=iCount%>.submit()">Click Me</a><td>
                </tr>
    </tbody></table> </form></td>
            <%
                }
            %>

        </table>

    <%
        } catch (Exception ex) {
            ex.printStackTrace();
    %>
    Ooops, something bad happened:
    <%
        } finally {
            if (resultSet != null)
                resultSet.close();
            if (statement != null)
                statement.close();
            if (connection != null)
                connection.close();
        }
    %>

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13844

From the attached image I can see that you can see firstname and last name value.This is because your input type is hidden

<input type="hidden" name="param1"
                value="<%=resultSet.getString("first_name")%>"></td>

Also you said here *Now I want first_name and last_name should appear as links and I found this Discussion, so I tried :*

so you should do like this

<a href="your _link"><%=resultSet.getString("first_name")%></a>

Upvotes: 1

Related Questions