Ajax- How To Get The Value Of My Username?
I have a JSP page that looks like this: The new username column allows me to input any string of text and when I hit update user button,it should update the table in my database.
Solution 1:
There is minor change required in update function to pass value.
you need to pass value in javascript function like this way.
<button onclick=update('abc','bcd')></button>
Check below code.
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><script>
function update(param, param1) {
var newuser = document.getElementsByName('NewUserName' + param)[0].value;
var currentuser = param1;
$.ajax({
type : "POST",
url : "update.jsp",
data : {
name : newuser,
current : currentuser
},
success : function(msg) {
alert("Data Updated: "+ msg);
location.reload();
},
error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
</script><form method="get" action="migratefolder.jsp"><%try {
Class.forName("com.mysql.jdbc.Driver");
String connectionURL ="jdbc:mysql://localhost:3306/test";
String username ="root";
String password ="root";
Connection conn = null;
conn =DriverManager.getConnection(connectionURL, "root", "root");
String query ="select ausername, ausertype from auser where AUSTATE='Y'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
String str ="<table border=2><tr><th>Username</th><th>New Username</th><th>User Type</th><th>Update</th></tr>";
int rowid =0;
String currentuser = null;
while (rs.next()) {
rowid++;
currentuser = rs.getString("ausername");
String autype = rs.getString("ausertype");
str +="<tr><td><input readonly type=\"value\" name=\""+ currentuser +"\" value="+ rs.getString("ausername") +" </td><td><input type=\"text\" name=\"NewUserName"+ rowid
+"\""+"value=\"\"></td> <td>";
if (autype.equalsIgnoreCase("Superuser")) {
str +="<input type=\"radio\" name=\"usertype"+ rowid
+"\" value=\"Superuser\" checked> Superuser ";
} else {
str +="<input type=\"radio\" name=\"usertype"+ rowid +"\" value=\"Superuser\" > Superuser ";
}
if (autype.equalsIgnoreCase("Basic")) {
str +=" <input type=\"radio\" name=\"usertype"+ rowid
+"\" value=\"Basic\" checked > Basic ";
} else {
str +=" <input type=\"radio\" name=\"usertype"+ rowid +"\" value=\"Basic\" > Basic ";
}
if (autype.equalsIgnoreCase("View")) {
str +=" <input type=\"radio\" name=\"usertype"+ rowid +"\" value=\"View\" checked> View ";
} else {
str +=" <input type=\"radio\" name=\"usertype"+ rowid +"\" value=\"View\" > View ";
}
str +="</td><td><button type=\"button\" onclick=\"update("+ rowid +",'"+currentuser+"')\">Update User</button></td> </tr>";
}
out.println(str);
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
Post a Comment for "Ajax- How To Get The Value Of My Username?"