如題,我做了一個網站,如下代碼,現在的問題是網站可以修改,但是一旦刷新或重新登錄後又恢復原狀了。
請問改寫什麼代碼才能保存網頁修改的信息?
<%@ page import ="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome to the TV_Play Web!</title>
<script type="text/javascript">
// 新增
function trdadd(){
flag=false;
document.getElementById("fid").style.display="block"; //控制顯示
chongzhi();
document.getElementById("aid").disabled=false; //重新啟用
}
//保存
function baocun(){
if(flag==false){
add(flag);
document.getElementById("fid").style.display="none";
}else{
add(flag);
document.getElementById("fid").style.display="none";
}
}
//重置
function chongzhi(){
document.getElementById("formid").reset();
}
//刪除
function deleteRow(input){
var s=input.parentNode.parentNode.rowIndex;
document.getElementById("tableid").deleteRow(s);
var num=document.getElementById("tableid").rows.length;
for(var i=1;i<num;i+=1){
table.rows[i].cells[0].innerHTML=i; //把每行的每一列設為i
}
alert("Delete Successfully !!"); //刪除成功
}
</script>
</head>
<body>
Welcome to the TV_Play Web!<br>
<input type="button" value="new" onclick="trdadd()">
<center>
<table id="tableid" border="1" cellpadding="0" cellspacing="0" width="100%" >
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>language</td>
<td>country_of_origin</td>
<td>genre</td>
<td align="center" width = "15%">operation</td> <!-- 操作 -->
</tr>
</thead>
</table>
<br>
<br>
<br>
<div id="fid" style="display : none">
<form id="formid">
<table id=aaid>
<tr>
<td colspan="6">name : <input id="aid" type="text" size="20%"> </td>
<td colspan="6">language : <input id="bid" type="text" size="20%"> </td>
</tr>
<tr>
<td colspan="6">country_of_origin : <input id="cid" type="text" size="20%"></td>
<td>genre :
<select id="eid" name="my_name">
<option value="1">Sitcom</option>
<option value="2">Horror</option>
<option value="3">Fantasy</option>
<option value="4">Science Fantasy</option>
<option value="5">Crime Drama</option>
<option value="6">Period Drama</option>
<option value="7">Family</option>
</select>
</td>
</tr>
</table>
<input type="button" value="save" onclick="baocun()">
<input type="reset" value="reset" onclick="chongzhi()"> <!-- 重置type類型必須為reset--> <!-- 重置 -->
</form>
</div>
</center>
<script type="text/javascript">
//全局變量
var table=document.getElementById("tableid");
var flag=false;
var getselectrow;
function getNum(){
var haoRow=table.rows[0];
return haoRow.cells.length;
}
//添加方法
function add(flag){
if(!flag){
// var num=getNum();
var row=table.insertRow(-1); //為-1時往下加(升序),為0時往上加(id降序)
var add1=row.insertCell(0);
var add2=row.insertCell(1);
var add3=row.insertCell(2);
var add4=row.insertCell(3);
var add5=row.insertCell(4);
var add6=row.insertCell(5);
add1.innerHTML=document.getElementById("tableid").rows.length-1;//不加-1時id從二開始;原因:標題占一個
add2.innerHTML=document.getElementById("aid").value;
add3.innerHTML=document.getElementById("bid").value;
add4.innerHTML=document.getElementById("cid").value;
var tall=document.getElementById("eid");
var index=tall.selectedIndex;// 當前坐標
var option=tall.options[index];
add5.innerHTML=option.text;
add6.innerHTML="<input type='button' value='revise' onclick='updateRow(this)'> <input type='button' value='delete' onclick='deleteRow(this)'>";
//alert(num);
alert("Add Successfully!!"); //添加成功
}else{
var row2=table.rows[getselectrow];//選中當前行
//把修改後的值設置到對應的文本框中
row2.cells[1].innerHTML=document.getElementById("aid").value;
row2.cells[2].innerHTML=document.getElementById("bid").value;
row2.cells[3].innerHTML=document.getElementById("cid").value;
var pall=document.getElementById("eid");
var index=pall.selectedIndex; //當前坐標
var option=pall.options[index];
row2.cells[4].innerHTML=option.text;
alert("Revise Successfully!!"); //修改成功
}
}
//修改
function updateRow(input){
flag=true
document.getElementById("aid").disabled=true; //不能啟用
document.getElementById("fid").style.display="block";
var i=input.parentNode.parentNode.rowIndex;
getselectrow=i;
//得到選中行的內容放到文本框
document.getElementById("aid").value=table.rows[i].cells[1].innerHTML;
document.getElementById("bid").value=table.rows[i].cells[2].innerHTML;
document.getElementById("cid").value=table.rows[i].cells[3].innerHTML;
var select=document.getElementById("eid");
var index=select.selectedIndex;// 當前坐標
var option= select.options[index];
option.text=table.rows[i].cells[4].innerHTML;
}
</script>
</body>
</html>
保存數據到數據庫中,每次刷新重新獲取數據