I am trying to make a forum in which as we submit the form the post should be saved into the database and appear below the form. It is getting saved into the database but appears only after we reload the page. Is there some way that it appears as we submit the form?
<%@ page import="java.sql.*"%>
<%@page import="org.json.*"%>
<%
System.out.println("Start");
String value = request.getParameter("jsonData");
JSONObject newObj = new JSONObject();
try {
newObj = new JSONObject(request.getParameter("jsonData"));
} catch (JSONException e) {
e.printStackTrace();
}
String title = "";
String tag = "";
String postData = "";
try {
title = newObj.getJSONObject("mydata").getString("title");
tag = newObj.getJSONObject("mydata").getString("tag");
postData = newObj.getJSONObject("mydata").getString("postData");
} catch (JSONException e) {
e.printStackTrace();
}
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=true", "root", "1111");
//System.out.println(con);
PreparedStatement ps = con.prepareStatement("insert into forumdb.userpost(title,post,tag) values(?,?,?)");
ps.setString(1, title);
ps.setString(2, postData);
ps.setString(3, tag);
ps.executeUpdate();
con.close();
} catch (Exception e) {
}
System.out.println("End");
response.setContentType("text/html");
response.getWriter().write("success");
%>
var request;
function postArticle() {
var title=document.postform.title.value;
var postDescription = CKEDITOR.instances.postDescription.getData();
var tag=document.postform.tag.value;
var myData = {"mydata": {"title": title, "tag": tag, "postData":postDescription}};
$.ajax({
type: "POST",
url: "/forum1/index.jsp",
data: {jsonData: JSON.stringify(myData)},
dataType: "json",
success: function(){
if(success==false)
{ location.reload(); }
}
});
}