简体   繁体   中英

Updating database with jquery ajax for jsp

I wish to create a simple script to store information dynamically without refreshing or redirecting to another page. I've sourced high and low for answers before coming here, and I followed this .

However, I'm still unable to get it to store data because I don't know where I've goen wrong. Also, I wish to ask the purpose of success: function(msg)

Thanks a lot!

Update 1: Got it to work after following Anoop's solutions. However, such a popup appears when the data is submitted

StaffReg.jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Staff Registration</title> </head> <body> <%-- HTTP header --%> <%response.addHeader("Cache-Control","no-cache"); response.addHeader("Pragma","no-cache"); response.addHeader("Expires","0"); %> <%-- Javascript --%> <script type="text/javascript" src="jquery-1.11.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#Register').click(function(e) { e.preventDefault(); $.ajax({ url: "StaffRegAuth.jsp", type: "post", data: { username: $('#username').val(), password: $('#password').val(), userGroup: $('#userGroup').val() }, success: function(msg) { alert(msg); } }); }); }); </script> <%-- Main body --%> <h1 align="center"> Account Registration: </h1> <div align="center"> <table style="width = 30%" > <tr> <td> User Name: </td> <td><input type="text" id="username"></td> </tr> <tr> <td> Password: </td> <td><input type="password" id="password"></td> </tr> <tr> <tr> <td> User Group: </td> <td><select name = "userGroup" id="userGroup"> <option value="1">Administrator </optin> <option value="2">Clerk </optin> <option value="3">Operations </optin> <option value="4">Sales </optin> </select></td> </tr> <tr> </table> <input type="submit" value="Register" id="Register"> </div> </body> </html> 

StaffRegAuth.jsp

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <%-- Imports --%> <%@ page import="java.sql.*"%> <%@ page import="java.util.*"%> <%@page import="javax.servlet.*" %> <%@page import="javax.servlet.http.*" %> <%-- HTTP header --%> <%response.addHeader("Cache-Control","no-cache"); response.addHeader("Pragma","no-cache"); response.addHeader("Expires","0"); %> <%-- Variables that will be written --%> <% String sUsername = request.getParameter("username"); String sPassword = request.getParameter("password"); int sUserGroup = Integer.parseInt(request.getParameter("userGroup")); %> <%-- Creating new staff account - writing --%> <% try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String conURL= "jdbc:odbc:HOD_DATA"; Connection con = DriverManager.getConnection(conURL); Statement st = con.createStatement(); int status = st.executeUpdate("insert into Staff(username, password, user_group) values('"+sUsername+"','"+sPassword+"',"+sUserGroup+")"); if(status>0){ //out.println("Update sucessful"); } else{ //out.println("Update unsuccessful"); } st.close(); con.close(); } catch(Exception e){ out.println(e); } %> </body> </html> 

  1. Remove the submit button and use a normal html button with Id as Register.
  2. Modify your html input types and replace the name attribute with id attribute. Ex: use id="username" instead of name="username"

Debugging: Use firebug to check what value is passed to server. Ensure the correct ajax request is submitted.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM