How can I submit the data from dynamic table into database?

烂漫一生 提交于 2020-06-17 01:00:13

问题


Currently I have a dynamic table inside the form using the following HTML and Javascript code:

<script type="text/javascript">
function addRows(){ 
	var table = document.getElementById('tourdaytbl');
	var rowCount = table.rows.length;
	var cellCount = table.rows[0].cells.length; 
	var row = table.insertRow(rowCount);
	for(var i =0; i <= cellCount; i++){
		var cell = 'cell'+i;
		cell = row.insertCell(i);
		var copycel = document.getElementById('col'+i).innerHTML;
		cell.innerHTML=copycel;
		
	}
}
function deleteRows(){
	var table = document.getElementById('tourdaytbl');
	var rowCount = table.rows.length;
	if(rowCount > '2'){
		var row = table.deleteRow(rowCount-1);
		rowCount--;
	}
	else{
		alert('There should be atleast one row');
	}
}
</script>
<form action="http://localhost:8080/project/sampleapi/tourday" method="post">    
	<table id="tourdaytbl">
		<tr>
			<th hidden>tourID</th>
			<th>startHotel</th>
			<th>endHotel</th>
			<th>routeNote</th> 
			<th>routeMap</th> 
		</tr> 
		<tr> 
			<td id="col0" hidden><input type="text" name="tourID" value="<%=request.getParameter("tourID")%>"/></td> 
			<td id="col1"><input type="text" name="startHotel" /></td> 
			<td id="col2"><input type="text" name="endHotel"  /></td>
			<td id="col3"><input type="text" name="routeNote" /></td>
			<td id="col4"><input type="text" name="endHotel" /></td> 
			
		</tr>  
	</table> 
	<table> 
		<tr> 
			<td><input type="button" value="Add Row" onclick="addRows()" /></td> 
			<td><input type="button" value="Delete Row" onclick="deleteRows()" /></td> 
			<td><input type="submit" value="Submit" /></td> 
		</tr>  
	</table> 
 </form> 

The following is the post method code:

@POST
@Produces(MediaType.APPLICATION_JSON)
public void addTourDay(     @FormParam("tourID") int tourID,
                            @FormParam("startHotel") String startHotel,
                            @FormParam("endHotel") String endHotel,
                            @FormParam("routeNote") String routeNote,
                            @FormParam("routeNote") String routeMap) throws ClassNotFoundException, SQLException {

    TourDay tourDay = new TourDay();

    tourDay.setTourID(tourID);
    tourDay.setStartHotel(startHotel);
    tourDay.setEndHotel(endHotel);
    tourDay.setRouteNote(routeNote);
    tourDay.setRouteMap(routeMap);

    TourDayDao.getInstance().add(tourDay);

    logger.info("Tour and Ride-Out was submitted to the database successfully!");

}

And the database dao:

public boolean add(TourDay tourDay) throws ClassNotFoundException, SQLException {

    if(tourDay != null) {

        Connection connection = Configs.getDbConnection();
        String sql =    "INSERT INTO `tbl_tour_day` (`tourID`, `startHotel`, `endHotel`, `routeNote`, `routeMap`)" + 
                "VALUES ( (SELECT tourID from tbl_tour_ride where tbl_tour_ride.tourID = ?), ?, ?, ?, ?)";
        PreparedStatement stmt = connection.prepareStatement(sql);

        stmt.setInt(1, tourDay.getTourID());
        stmt.setString(2, tourDay.getStartHotel());
        stmt.setString(3, tourDay.getEndHotel());
        stmt.setString(4, tourDay.getRouteNote());
        stmt.setString(5, tourDay.getRouteMap());

        int count = stmt.executeUpdate();

        if(count > 0) {
            System.out.println("Successfully Added");
            return true;
        } else {
            System.out.println("Error occured");
        }

    }

    return false;

}

The problem I am facing is that when I click on the Submit button only the first row is being stored in the database. The rest of the dynamic rows are not being stored. How can I fix this issue?

来源:https://stackoverflow.com/questions/61933233/how-can-i-submit-the-data-from-dynamic-table-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!