How to call jsp onclick / on form submit method
I have a jsp page with this code:
<script type="text/javascript">
function getWithdrawAmmount()
{
var withdraw=document.forms["WithdrawDeposit"]["AmountToWithdraw"].value;
document.getElementById('hidden').type = withdraw;
}
</script>
<form method="POST" name="WithdrawDeposit" onsubmit="getWithdrawAmmount()">
<table>
<tr><td><input type="text" size=5 name="AmountToWithdraw"></td>
<td><input type="button" value="Withdraw"></td></tr>
</table>
</form>
<input type="hidden" name="hidden" value="">
<% String AmountWithdraw = request.getParameter("hidden"); %>
<%!
public void Withdraw(){
int Amount = Integer.parseInt("AmountWithdraw");
Deposit deposit = new Deposit();
deposit.WithdrawMoney(AmountWithdraw);
} %>
I need to activate the Withdraw () method in the submit form and get the text input. javascript holds the value inserted into 'hidden' and I can access it later. but I can’t call: <% Withdraw (); %> from inside javascript.
How can I call Withdraw () after clicking a button?
10x
Firstly, your line of code is having problems.
document.getElementById('hidden').type = withdraw;
It searches for an element with an identifier hidden. Not a name, not id. Therefore, add the identifier to the element you are referring to.
Secondly, you set the type. You do not want to set the value?
So HTML will look like
<input type="hidden" name="hidden" id="hidden" value="" />
and javascript will
document.getElementById('hidden').value = withdraw;
Now, if you want to call a function on the server, you need to either send the form back or make an Ajax call.