<form id="form1" name="form1" method="post">
User Name:<input type="text" name="user" id="user"/>
Password <input type="password" name="pass" id="pass"/>
Phone <input type="number" name="phone" id="phone"/>
Email <input type="email" name="email" id="email"/>
State <input type="text" name="state" id="text"/>
<input type="button" onclick="return register();" value="submit"/><br/>
<div id="welcometext"></div>
</form>
function register() {
var val = $('#form1 :input').serialize*();
$.post('ActionServlet', { data: val }, function (responseText) {
$('#welcometext').text(responseText);
});
}
现在我想在Servlet中获取这些数据并添加到mysql数据库并获取数据并将该数据响应到div标签中的表单。请帮帮我。
答案 0 :(得分:0)
不确定您是否使用弹簧控制器,但希望此片段对您有所帮助
var formFields = {
"user" : $("#user").val(),
"password" :$("#pass").val(),
//.... other fields
}
function register() {
$.post('ActionServlet', { data: formFields}, function (responseText) {
$('#welcometext').text(responseText);
});
}
的Servlet
import javax.servlet.*;
import javax.servlet.http.*;
public class ActionServlet extends HttpServlet {
public String getData(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("parameter you want to retrive");
return "response you want to send";
}
注意:您的表单有一个字段密码,如果您使用ajax发送此数据,则在请求中将显示作为安全标识的密码。因此,最好使用表单submit
而不是使用ajax来提交表单
答案 1 :(得分:0)
你可以使用jquery ajaxForm http://malsup.com/jquery/form/
$('#form1').ajaxForm(function(response){
//response from server
});