1

I want to autofill the form values based on the login id in the database but my Ajax code is not working. After entering the login id and clicking anywhere outside the login textbox, other values should automatically be filled in the form. When I am entering the loginid and clicking outside the text box, other fields are getting highlighted but data is not retrieved from the database.

//This is my Ajax code with jQuery
//"lid" is the name of the login field in the form
$(document).ready(function()  {
            $("#eid").on("focusout",function() {
            var id=$("#eid").val();


            if(id==="")
            {
            $("#error").html("<p>This is a required field</p>");
            $("#f2").trigger("reset");
            return false;
            }

            else
            {
            $("#error").html("");
            $.ajax({
            url:"getAutofillValue.jsp",
            data:{lid:id},
            method:"POST",
            success:function(data)
            {
            var fdata=$.trim(data);
            var sp=fdata.split(",");
            $("#sit").val(sp[0]);
            $("#super").val(sp[1]);
            $("#log").val(sp[2]);
            $("#depart").val(sp[3]);

            }


            });
            }
            });
            });






Following is my getAutofillValue.jsp page to retrieve data from database based on login id:
    

<% 
        
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasenum5", "root", "root");
            String id = request.getParameter("lid");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from employee where Login_id="+id+"");
            while(rs.next())
            {
            out.print(rs.getString("site")+","+rs.getString("name")+","+rs.getInt("employee_id")+","+rs.getString("department"));
            }
            st.close();
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        
        %>
Hliquid
  • 11
  • 3

0 Answers0