Question

I want to check something server side when I press/select some value in the drop down list. How can I do that ?

I tried as follows :

code : for list
    <select id="stream" name="current_session" >
                <option value="Winter">Winter</option>
                <option value="Monsoon">Monsoon</option>
    </select>

code : for button
<form>
  <input type="button " name="btn" value ="button">

code : for sriplet
  if(request.getParameter("current_session")!=null) 
 {
      system.out.println("helo ur list test is okey ");
 }
  if(request.getParameter("btn")!=null) 
 {
      system.out.println("helo ur test for button is okey ");
 }

note : when im using a button it is working fine when i press the button, but when i select the list its not working.

Was it helpful?

Solution

Attach an on-change listener to your select tag which can fire an ajax request and then you can perform the server-side check and return the response which can be processed on the client-side.

JQuery change

<select id="stream" name="current_session" >
                <option value="Winter">Winter</option>
                <option value="Monsoon">Monsoon</option>
    </select>

<script>
$( "#stream" ).change(function() {
  var selectedVal=$("#stream option:selected").val();
  $.ajax({
     url:"checkonserver.jsp?current_session="+selectedVal,
     success:function(msg){
             alert(msg);
     }
  });
});
</script>

checkonserver.jsp

<% if(request.getParameter("current_session").toString().equals("Winter")){%>
     It's COLD
<%}else{%>
     It's HOT
<%}%>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top