user3496683
user3496683

Reputation: 25

Passing Value of DropDown list to if statement

I want to get the value of the selected item in Dropdown list and pass it automatically to the if statement without using a button.

If I select 1 item I want it to automatically run and execute the if statement regarding to the value you selected.

If I choose All Members it will automatically show all members, and If I choose department it will show all Department. I already done the sql statement I just need to pass it in if statement and run it automatically after I choose the value.

This is my code.

<sql:query dataSource="${snapshot}" var="sql">
    SELECT * from employee;
    </sql:query>

      <select name="selection">
                    <option>Select Category</option>
                    <option value="AllMembers">All Members</option>
                    <option value="AllDepartment">All Department</option>
            </select>
      </p>
      <% 

       String x = request.getParameter("selection");
      if(x=="MembersPerHub"){
          %>


        <table border=1>
            <thead>
                <tr>
                    <th>Employee Name</th>
                         </tr>
            </thead>
            <tbody>
            <c:forEach var="a" items="${sql.rows}">
              <%--   <c:forEach items="${employee}" var="a"> --%>
                    <tr>
                        <td><c:out value="${a.empname}" /></td>

                    </tr>
                </c:forEach>
            </tbody>
        </table>
        <% }%>

Upvotes: 1

Views: 1175

Answers (1)

Simon
Simon

Reputation: 6363

It's important to be clear about what happens on the client side and what happens on the server side.

You can use Javascript events to detect when the selection is changed.

Javascript is executed on the client side so the Javascript code can't talk directly to the Java or JSP code because Java and JSP code is executed on the server side.

You have to have your Javascript event handler code make a request to your server with information about the selection that was made and then you have the JSP code that renders the page extract that information from the request and use it in your if statement.

Upvotes: 1

Related Questions