使用ajax发送多个参数并将数据保存到数据库....在数据包的情况下我希望值在表单上保持不变意味着我不希望我的表单重新加载所以,我怎么可能#39; ma新手请提出宝贵意见
onsubmitbtn调用提交按钮
function onsubmitbtn(){
var packet = document.getElementById("packet").value;
var name = document.getElementById("name").value;
var number = document.getElementById("number").value;
var sub = document.getElementById("sub").value;
var zipcode = document.getElementById("zcode").value;
var city = document.getElementById("city").value;
var weight = document.getElementById("weight").value;
var data = "packet=" +packet+ "&name="+name+ "&number="+number+ "&sub="+sub+ "&zipcode="+zipcode+ "&city="+city+ "&weight="+weight;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
var response = request.responseText;
}
request.open("get", "PickUpInvertFormSubmit.jsp?"+data, true);
request.send();
}
我想在我的ajax中发送多个参数并将值保存在我的数据库中。如果在这种情况下数据包不止一个我想要提交但输入字段中的值将保持不变
用于将数据插入数据库的jsp代码
String name = request.getParameter("name");
String[] splt = client.replaceAll("\\s+", "").split("\\|");
String number = request.getParameter("number");
String sub = request.getParameter("sub");
String zip = request.getParameter("zipcode");
int z = Integer.valueOf(zip);
String city = request.getParameter("city");
String pkt = request.getParameter("packet");
int p = Integer.valueOf(pcs);
String weight = request.getParameter("weight");
double w = Double.valueOf(weight);
Dbcon dbc = new Dbcon();
Connection con = dbc.ConnDb();
String query = "insert query"
+ "VALUES (?,?,?,?,?,?,?,CURRENT_DATE,CURRENT_DATE,CURRENT_TIMESTAMP)";
PreparedStatement psmt = null;
try {
psmt = con.prepareStatement(query);
psmt.setString(1, ....);
psmt.setString(2, .......);
psmt.setString(3, ........);
psmt.setInt(4, .......);
psmt.setString(5, .......);
psmt.setInt(6, ..........);
psmt.setDouble(7, .......);
psmt.executeUpdate();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
} finally {
dbc.disconnect(null, null, null, psmt);
}
}
当我在行动中使用此代码时,此代码可以正常工作
onsubmit按钮事件我的表单字段被刷新,数据库中没有数据保存...
答案 0 :(得分:0)
使用此
function onsubmitbtn() {
var packet = document.getElementById("packet").value;
var name = document.getElementById("name").value;
var number = document.getElementById("number").value;
var sub = document.getElementById("sub").value;
var zipcode = document.getElementById("zcode").value;
var city = document.getElementById("city").value;
var weight = document.getElementById("weight").value;
$.ajax({
type: 'POST',
cache: false,
url: "PickUpInvertFormSubmit.jsp",
data: {
"packet": packet,
"name": name,
"number": number,
"sub": sub,
"zipcode": zipcode,
"city": city,
"weight": weight
},
success: function (data, textStatus, jqXHR) {
successMethod(data);
},
error: function (jqXHR, textStatus, errorThrown) {
errorMethod(errorThrown);
}
});
}
您的数据在提交后无法更改,也无法加入书签。
您还可以在alert("success: " + data);
函数的success
值中使用ajax
,并在ajax方法的error
中使用<head>
<script type="text/javascript" src="jquery-3.2.1.js"></script>
</head>
。
使用此功能,数据不会附加到浏览器的URL,您无需修改控制器/ jsp。
此方法将在后台调用URL,您可以在success方法中执行清理。
还将文档的head标记中的jQuery导入为
create table t1 (id number);
create table t2 (id number, constraint tpk primary key (id));
insert into t1 values (1);
insert into t1 values (1);
commit;
merge into t2
using t1
on (t2.id = t1.id)
when not matched then insert values (t1.id);
SQL Error: ORA-00001: unique constraint (TPK) violated
您可以从这里下载jQuery Download jQuery | jQuery
希望这会有所帮助:)