0
I would like to know how I get the value of select option and inputo within a variable in jsp. I’m trying the following code plus this giving error :
Here I fill select with data coming from the database.
<select id="STR_MAC" style="width: 600px;" name="STR_MAC" onchange="update()">
                        <%
                            String qry_types2 = "SELECT DISTINCT MACROTECNOLOGIA FROM EPE_VW_PRT_MAPE_ALLOC ORDER BY MACROTECNOLOGIA";
                        ResultSet rsSTR_MAC = stm.executeQuery(qry_types2);
                        while (rsSTR_MAC.next()) {
                            if (rsSTR_MAC.getString("MACROTECNOLOGIA").equals(request.getParameter("STR_MAC"))) {
                                out.println("<option value='" + rsSTR_MAC.getString("MACROTECNOLOGIA") + "' selected=true>"
                                + rsSTR_MAC.getString("MACROTECNOLOGIA") + "</option>");
                            } else {
                                out.println("<option value='" + rsSTR_MAC.getString("MACROTECNOLOGIA") + "' >"
                                + rsSTR_MAC.getString("MACROTECNOLOGIA") + "</option>");
                            }
                        }
                        rsSTR_MAC.close();
                        %>
                    </select>
And here’s where I’m trying to pass the value of the select option to my variable but this is giving error.
<script type="text/javascript">
    <% 
    String STR_MAC = new String();
    
        function update() {
            var select = document.getElementById('STR_MAC');
            var text = select.options[select.selectedIndex].text;
            console.log(text);
        }
        update();
        
        STR_MAC = text;
        
        System.out.println("Esse eu peguei da tela : " + STR_MAC);
        %>
    </script>
Here follows the error that is being generated on my page :
org.apache.jasper.JasperException: Unable to compile class for JSP: 
An error occurred at line: 161 in the jsp file: /kpi/chart_people_knowledge_risk.jsp
Syntax error on token "function", new expected
158:    <% 
159:    String STR_MAC = new String();
160:    
161:        function update() {
162:            var select = document.getElementById('STR_MAC');
163:            var text = select.options[select.selectedIndex].text;
164:            console.log(text);
Thank you all and thank you for your attention.
And in this case how could I take this value from the select option and pass to a jsp variable ?
– salgadotec
No onchange makes a request using fetch api passing the value, so you can catch.
– Franklin Barreto