I want to call a javascript function that returns a value and then put that value in an if statement. There are two radio buttons in the HTML and the javascript checks to see which one is clicked. After that, the JSP compares it to either 'customers' or 'company' and does the appropriate SQL Query.
Javascript:
function corc{
var value;
if(document.getElementById('cust').checked){
value='customer';
return value;
}else if(document.getElmentById('comp').checked){
value='company';
return value;
}
}
JSP:
if(%>corc();<%.equals("customer")){
String sqlqueryCommand = "SELECT * from customer where login='" + v1 + "' and password='" + v2 + "'";
}else if (%>corc();<%.equals("company")){
String sqlqueryCommand = "SELECT * from company where login='" + v1 + "' and password='" + v2 + "'";
}
Best Answer
You can not call JavaScript function in if statement of JSP, because JSP is executed at the server side and JavaScript is executed at client side.
You have to trigger event when the one of the radio button is clicked, using
onclick
event you can call functioncorc()
.Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?
JSP code:
onclick
event I assignedhandleClick
function and passedthis.id
, parameterthis.id
is used to pass theid
attribute of the clicked radio button.JavaScript code:
tableName
to query.Related links