简体   繁体   中英

ajax in jsp call sql query when select dropdown value

need help to get value from sql table in jsp when select from dropdown.

when i select CL then I want to run a query in jsp get all record from sql table.

 $(document).ready(function() { $leavetype= $('.leavetype'); $leavetype.change(function (){ var value = $('.leavetype').val(); $.ajax({ type: "post", url: "applicationformuser.jsp", //this is my servlet data: {leavetype : value}, success: function(msg){ alert(msg.data); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <select class="leavetype" name="leavetype"style="position:absolute;margin-left:160px;margin-top:160px;height:30px;width:300px;"> <option value="CL">CL</option> <option value="ML">ML</option> <option value="SL">SL</option> <option value="EL/PL">EL/PL</option> <option value="CPL">CPL</option> </select> 

try this

<select class="leavetype" name="leavetype" style="position:absolute;margin-left:160px;margin-top:160px;height:30px;width:300px;" onchange="GetAllDetails(this.value);">
        <option value="CL">CL</option>
        <option value="ML">ML</option>
        <option value="SL">SL</option>
        <option value="EL/PL">EL/PL</option>
        <option value="CPL">CPL</option>
    </select>


  function GetAllDetails(value) {
        $.ajax({
            type: "post",
            url: "applicationformuser.jsp", //this is my servlet
            data: { leavetype: value },
            success: function (msg) {
                alert(msg.data);
            }
        });

    }

You ajax code works fine,you just need to get it in jsp code, you can get parameter using request.getParameter() :

<% 
String leavetype = request.getParameter("leavetype");//after get value,you can pass it to query sql.
%>

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