Reputation: 581
I want to generate a SQL query when I press the submit button and show the number of rows it returns without refreshing. I've manage to do this below, but it refreshes and clears everything and the value is always 0
. I'm using Tomcat and NetBeans.
I'm using the submit function because after I solve this I will return false if something happens.
The msg
span is where the rows returned are going to be displayed.
Funciones.setSql
generates the SQL using the value on the option selected and stores in a static String. Then the Funciones.count()
just makes the query and stores the number of rows in a variable called numF
.
Funciones.lista()
generates all the data in the database using one specific field and setting a value (id).
JSP:
<div data-role="page" id="realizadas">
<%@include file="header.jsp"%>
<div data-role="content">
<form name="tareasr" id="tareasr" data-mini="true" method="post">
<fieldset data-mini="true" data-role="controlgroup" data-type="horizontal">
<select data-inline="true" name="vina" id="vina">
<option value="0">VIÑAS</option>
<%=Funciones.lista("VINA","option")%>
</select>
</fieldset>
<input data-icon="search" type="submit" value="Buscar" />
</form>
<b><span id="msg"></span></b>
<script>
$("#tareasr").submit(function() {
<jsp:useBean id="formHandler" class="ges.FormBean">
<jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>
var men="Num: ";
<%Funciones.setSql(formHandler.getVina());
Funciones.count();%>
men+=<%=Funciones.numF%>;
alert(men);
$("#msg").text(men).show().fadeOut(3000);
});
</script>
</div>
</div>
Upvotes: 1
Views: 1314
Reputation: 691943
You're mixing Java code and JavaScript code as if they were the same thing. Java code inside a JSP should be avoided, and if it's there, it must be inside scriptlet tags (<% ... %>
). Thsi Java code is executed at server-side, when the JSP is executed and the HTML and JavaScript code is generated.
Then, in the browser, JavaScript code can be executed. This JavaScript code can't invoke methods of Java objects which were used to generate HTML at server-side. Java doesn't exist in the browser. If you need to invoke server-side Java code from the browser, you must send a new HTTP request to the server (by clicking a link, submitting a form, or sending an AJAX request).
Upvotes: 1