how to pass the value from Jquery to jsp?

My jQuery code:

function check_availability(){   

         //get the username   
         alert("hai");
         var result ="";
         var CompanyCode = $('#CompanyCode').val();   
     alert("Jquery " +CompanyCode);
     //use ajax to run the check   
         $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   
             function(result){   
                 //if the result is 1   
                 alert("result"+result);
                 if(result == 1){   
                     //show that the username is available   
                     $('#username_availability_result').html(CompanyCode + ' is Available');   
                 }else{   
                     //show that the username is NOT available   
                     $('#username_availability_result').html(CompanyCode + ' is not Available');   
                 }   
         });   

}

checkUserName.jsp:

String companyCode = request.getParameter("CompanyCode");
out.println("companyCode"+companyCode);

rs = dbAccess.executeQuery("select companycode from yosemitecompany where companycode = '"+companyCode+"'");

How to get the values from jQuery? I need to get the answer in the JSP page for checking ,whether the record is present or not in the table. I am not getting the value from the request.getParamater() method. Is this any other way to check that ?

Upvotes: 1

Views: 10309

Answers (3)

Nitin
Nitin

Reputation: 1

It seems that CompanyCode is the id attribute.So just create an attribute name="something" then use request.getParameter("something").I hope it will work

Upvotes: 0

Mohan
Mohan

Reputation: 1

Your code is absolutely valid. Only thing as said above the variable naming may the issue. You can try the other option from jquery api,

$.post("checkUserName.jsp", $('#CompanyCode').serialize(),..

This should take care of your issue. Because query string values should be quoted ('') properly. This serialize method would take care of that.

Upvotes: 0

Hank
Hank

Reputation: 547

Instead of

     $.post("checkUserName.jsp", { CompanyCode: CompanyCode },   

Try

     $.post("checkUserName.jsp", { Code: CompanyCode },   

Perhaps it's an issue with naming the keys the same as that var...

Upvotes: 2

Related Questions