I have used ajax with jquery and my ajax url is calling another jsp page which submits form data to database the problem is whenever I'm running the insert query, its replying "Null values can't be inserted to database" kinda exception, but whenever I'm reloading same page and clicking submit button, its getting submitted to database. I think the problem is with something like postback method, I'm new to ajax so please need your help...
<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>