0

I have a dropdownbox written in HTML which takes different categories from a MYSQL-database.

<td><form:select path="categoryName">
    <form:option value="NONE" label="--- Select Category ---" ></form:option>
    <form:options items="${categoryList2}" ></form:options>
</form:select></td>

The question I have is if I can use the selected category from that dropdownbox and use it in javascript code. Is it possible or do I need to re-create the box in html to javascript instead.

Many comments say that I should have the same problem as the other forum-thread. But I want to use this in a SQL-prepeared statement from java. So far I have this:

            var e = document.getElementById("categoryName");
        var selectItem = e.options[e.selectedIndex].text;

        var theData = [ // Start of JavaScript data object
        <%
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pyramid", "root", "test");
            PreparedStatement ps = con.prepareStatement("SELECT timeStamp, value FROM status WHERE idCategory = (SELECT idCategory FROM  category WHERE name =*' "+ %> + selectItem + <% + " '*)");
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
        %>
                [" <%= rs.getString(1)%>",<%= rs.getString(2)%>],
        <%
            };

The part where I wanna use the selectItem in the preaperedStatement does not work. Please help someone

Thanks

benskiiii
  • 439
  • 3
  • 11
  • 23
  • 1
    I don't understand what *"re-create the box in html to javascript instead"* means. – Felix Kling Jun 18 '13 at 11:47
  • It means that if needed, I can remove the box in HTML and create one in javascript, if the transfer does not work. Sorry for bad english. – benskiiii Jun 18 '13 at 11:49
  • and Its not a duplicate, he did his box in javascript, mine is in HTML... – benskiiii Jun 18 '13 at 11:50
  • The code for the select box in the accepted answer is HTML. He uses JavaScript to access the select box via the DOM interface. Whether the element is created by parsing HTML or by creating it directly with JavaScript does not make a difference. If you are unfamiliar with DOM, you might want to read about it first: https://developer.mozilla.org/en/docs/DOM. – Felix Kling Jun 18 '13 at 11:52
  • Okay, thanks Im gonna read about it. – benskiiii Jun 18 '13 at 12:00
  • If I understand your question correctly, you are wanting to get the selected value from a html select element using native javascript. If so then this answer might help. http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-of-dropdownlist-using-javascript – roughcoder Jun 18 '13 at 11:52
  • Regarding your edit, you cannot mix JavaScript and Java like that. The Java commands are executed on the server, *before* the JavaScript is executed on the client. If you want to run the DB query whenever the value of the select changes, have a look at Ajax: http://en.wikipedia.org/wiki/Ajax_(programming). – Felix Kling Jun 19 '13 at 08:16
  • Im thinking of using google own's API on getting json-data through AJAX and then put it in a datatable. In the url "url:"getData.php"," I instead put in my controllers url, that should work, right? – benskiiii Jun 19 '13 at 09:03

0 Answers0