2013-07-22 30 views
1

我用jQuery的ajax和我的Ajax网址调用另一个jsp页面提交表单数据到数据库问题是每当我运行插入查询,它的答复“空值不能插入到数据库”有点例外,但每当我重新加载相同的页面并点击提交按钮,它就会被提交到数据库。 我认为这个问题是像回传方法, 我是新来的阿贾克斯所以请需要你的帮助......如何处理jquery ajax与数据库更新的jsp?

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#save').click(function() 
     { 
      $.ajax({ 
       type: "post", 
       url: "test.jsp", //this is my servlet 
       data: "input=" +$('#id').val()+"&output="+$('#op').val(), 
       success: function(msg){  
         $('#output').append(msg); 
       } 
      }); 
     }); 

    }); 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 
</head> 
<body> 
    input:<input id="id" type="text" name="" value="" /><br></br> 
    output:<input id="op" type="text" name="" value="" /><br></br> 
    <input type="button" value="save" name="save" id="save"/> 
    <div id="output"></div> 
</body> 

JSP:

<%@page import ="com.connectionUtil.ConnectionUtil"%> 
<!DOCTYPE html> 
<script src="js/jquery.min.js" type="text/javascript"></script> 
<% String id = request.getParameter("input"); 
    try { 
     Connection con = ConnectionUtil.getConnection(); 
     String sql = "insert into test(ID,...) values ('" + id + "',...)"; 
     PreparedStatement ps = con.prepareStatement(sql); 
     ps.executeUpdate(); 
    } catch (SQLException sqe) { 
     out.println(sqe); 
    }%> 
<body> <h4><%=id%> is stored </h4> </body> </html> 
+0

带'id ='call''的元素在哪里? – anu

+0

@anu对不起,我编辑了它.. – kAmol

+0

检查控制台这个ajax调用及其响应。它会给你更多关于这个问题的想法 – anu

回答

3

你的ajax代码是错误的。它应该是这样的:

$.ajax({ 
    type: "post", 
    url: "test.jsp", //this is my servlet 
    data: {input : $('#id').val(), output: $('#op').val()}, 
    success: function(msg){  
     $('#output').append(msg); 
    } 
}); 

然后使用request.getParameter("input")来获取参数值

UPDATE:请注意,我错过了在该行数据的末尾逗号(,):. ...

更新2:这是你的代码没有连接数据库的工作演示...

的index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#save').click(function() 
     { 
      $.ajax({ 
       type: "post", 
       url: "test.jsp", //this is my servlet 
       data: { 
        input: $('#id').val(), 
        output: $('#op').val() 
       }, 
       success: function(msg){  
         $('#output').append(msg); 
       } 
      }); 
     }); 

    }); 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 
</head> 
<body> 
    input:<input id="id" type="text" name="" value="" /><br></br> 
    output:<input id="op" type="text" name="" value="" /><br></br> 
    <input type="button" value="save" name="save" id="save"/> 
    <div id="output"></div> 
</body> 

test.jsp的:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<% String id = request.getParameter("input"); 
String output = request.getParameter("output"); 
%> 
<%=id%> is stored <br/>output is:<%=output%> 

初始屏幕显示这一点:

enter image description here

和插入数据和按压按钮save后它显示这样的:

enter image description here

+0

值是当访问加载页面并提交和另一件事是我编辑的代码,如你所说,但没有变化的输出.. 。 – kAmol

+0

你是否设法让它工作? – MaVRoSCy

+0

** Thanx MaVRoSCy ... ** 您的更新代码正在工作。 再次感谢。 – kAmol

1

对不起,我不会张贴此作为答案,但我必须这样做,因为我没有权利在我的帐户中将其作为评论发布,但我确信您会混淆点击的内容。 #callelementdiv不是实际的按钮,但它也可以。您能否请您发布您的test.jsp,以便我们可以看到代码,因为我怀疑是否会导致此问题。

我想你已经编辑了你的问题,所以我认为我的回答是浪费。

+0

'code' <%@ page import =“com.connectionUtil.ConnectionUtil”%> <!DOCTYPE html> <% String id = request.getParameter(“input”); ... 尝试连接con = ConnectionUtil.getConnection(); String sql =“insert into test(ID,...)values('”+ id +“',...)”; PreparedStatement ps = con.prepareStatement(sql); ps。的executeUpdate(); catch(SQLException sqe){ out.println(sqe); } %>

<%=id%>存储

kAmol