0

I have the following front end code, I need to get the textfield(id=uid) value and parse it to the servlet and based on that value, fill the other two textfields(It's a Search Function), But following code only retrieves the values, couldn't send the "uid". How could I do that? Please help me.

    <script type="text/javascript">  
    $(document).ready(function(){   
            $('#getData').click(function(){ 

                $.ajax({  
                    url:'JsonServlet',  
                    type:'post', 
                    dataType: 'json',  
                    success: function(data) {  

                        $('#uname').val(data.uname);  
                        $('#uadd').val(data.uadd);
                    }  
                });  
            });  
    });  
</script>  

</head>  
<body>  

    UserID:<input name="userid" type="text" id="uid"/><br/>  
    Name:<input type="text" id="uname"/> 
    Address:<input type="text" id="uadd"/> 

    <input type="button" id="getData" value="Get Data"/>  

My servlet Code looks like this

response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String userid = request.getParameter("userid");
            ResultSet rs = db.selectQuery("select * from tbl_user where userid = '"+userid+"'");
            JSONObject json = new JSONObject();
            while (rs.next()) {
                json.put("uname", rs.getString("username"));
                json.put("uadd", rs.getString("useraddress"));

            }
            //json.put("uname", "user1");
               // json.put("uadd", "address1");
            out.print(json);

        } catch (Exception e) {
            e.printStackTrace();
        } 

1 Answer 1

1

your ajax should like this..

             $.ajax({  
                    url:'JsonServlet?userid='+document.getElementById("uid").value,  
                    type:'post', 
                    dataType: 'json',  
                    success: function(data) {  

                        $('#uname').val(data.uname);  
                        $('#uadd').val(data.uadd);
                    }  
                });  
            });  

the you should able to use String userid = request.getParameter("userid");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.