簡體   English   中英

從jsp頁表獲取價值

[英]Getting value from jsp page table

我想從jsp頁表中獲取值,並使用reservations.jsp插入數據庫。 下面的代碼我可以在表格中獲得名為“Buy”的選項部分打印出數據庫,但我無法在表中獲得activityId部分。 它在數據庫中返回null。 在reservations.jsp中不要讀取actvityId1。 我認為問題出在activityid1部分,代碼不包括像“name ='buy'”這樣的代碼。 如何獲得activityId1值? jsp頁面

music.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ page import ="java.sql.*" %>




<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 
    <form method="post" action="reservations.jsp">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Book Ticket</title>
    </head>

    <center>    
        <table border="1" width="30%" height="30%">
            <th><font color='#D18603'>ActivityID</font>
            <th><font color='#D18603'>Type</font></th>
            <th><font color='#D18603'>Description</font></th>
            <th><font color='#D18603'>City</font></th>
            <th><font color='#D18603'>Location</font></th>
            <th><font color='#D18603'>Date</font></th>
            <th><font color='#D18603'>Price</font></th>
            <th><font color='#D18603'>Buy</font>
                <form action="some.jsp">

                    </tr>

                    <form method="post">



                        <%
                            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

                            Statement st = con.createStatement();
                            ResultSet rs;
                            rs = st.executeQuery("select * from activities where type='müzik'");
                            while (rs.next()) {

                                String activityid1 = rs.getString("id");
                                String type1 = rs.getString("type");
                                String description1 = rs.getString("description");
                                String city1 = rs.getString("city");
                                String location1 = rs.getString("location");
                                String date1 = rs.getString("date");
                                String price1 = rs.getString("price");

                                out.println("<tr>");
                                out.println("<td>" + activityid1 + "</td>");
                                out.println("<td>" + type1 + "</td>");
                                out.println("<td>" + description1 + "</td>");
                                out.println("<td>" + city1 + "</td>");
                                out.println("<td>" + location1 + "</td>");
                                out.println("<td>" + date1 + "</td>");
                                out.println("<td>" + price1 + "</td>");
                                out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");
                                out.println("</tr>");

                            }
                            st.close();

                        %>


                        </center>
                        </table>
                        <tr>
                        <td><input type="reset" value="Reset" /></td>
                        </tr> 
                    </form>
                    <br><br><a href='logout.jsp'>Log out</a>
                </form>
                </body>
                </html>

reservations.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%
request.getParameter("activityid1");
request.getParameter("buy");
String username = (String) request.getSession().getAttribute("username");

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

String sorgu = "INSERT INTO reservation(id,username,buy) VALUES ('" + activityid1 + "', '" + username + "','" + request.getParameter("buy") + "') ";

java.sql.Statement st = con.createStatement();

int rowNum = st.executeUpdate(sorgu);
response.sendRedirect("paypal.html");
st.close();
%>

activityid1不是表單的一部分。 它的值不隨請求一起發送。 您需要將其添加為表單的隱藏元素:

out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'>  <input type=\"hidden\" id=\"activityid1Value\" name=\"activityid1Hidden\" value=\""+activityid1 +"\"></form></b>");

並檢索它:

request.getParameter("activityid1Hidden");

希望我幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM