WordToTheWise
WordToTheWise

Reputation: 93

how to invoke a jsp method onclick/on form submit

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 on form submit and get the text input. the javascript hold the value inserted in 'hidden' and i can access it later. but i can't call to : <% Withdraw(); %> from inside javascript.

how can i call Withdraw() after button click?

10x

Upvotes: 3

Views: 20064

Answers (1)

epascarello
epascarello

Reputation: 207501

First off your line of code has issues

document.getElementById('hidden').type = withdraw;

It is looking for an element with an id of hidden. Not a name, an id. So add an id to the element you are referencing.

Second you are setting a type. Don't you want to set the value?

So the HTML would look like

<input type="hidden" name="hidden" id="hidden" value="" />

and the JavaScript would be

document.getElementById('hidden').value = withdraw;

Now if you want to call a function on the server, you either need to post back the form or make an Ajax call.

Upvotes: 3

Related Questions