• Post Reply Bookmark Topic Watch Topic
  • New Topic

new values of javascript variables not being considered for 2nd request  RSS feed

Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Sorry for posting the same content under a different subject.)

Dear Bear Bibeault,
Thanks for your explanation regarding "sending variables along with html content".Now I�ve got a clear idea of what a browser understands and what it doesn�t . But now I�ve another problem�
When the user clicks on the checkout button for the first time , if purchase amount is greater than balance then the alert message �Insufficient Balance� is displayed.
Seeing this alert message the user modifies his/her purchase requests so that purchase amount is less than balance.
To modify a (purchase/sell)request , Modify.jsp and SaveChanges.java servlet are used.
SaveChanges.java makes the required changes to the Requests object(it�s a Model, shoppingcart kind of object) and forwards the httprequest to DisReq.jsp

When the user clicks on checkout button(for the 2nd time) after modifying purchase requests, the browser uses the old values of script variables puramt and balamt (used during first checkout) to check whether purchase amount< balance

<%--DisReq.jsp displays the Buy/Sell stocks requests so far placed by the user--%>
<%--Allows the user to remove or modify a desired request, place more requests and checkout--%>

<body bgcolor="#6495ed" text="#000000">

<%@ page import="java.util.*" %>
<%@ page import="client.model.*" %>

<%Requests req=(Requests)session.getAttribute("SHARES");
<%--Requests.java is a model (MVC) which acts like a shopping cart
req.calPurchase() calculates the purchase amount--%>
<script language="javascript" >
function checkBal()
<%--req.getAmt() and req.getBal() return purchase amount and balance respectively--%>
var puramt = <%=req.getAmt()%>;
var balamt=<%=Integer.parseInt(req.getBal())%>;
alert("Insufficient Balance");
return false;


<form method="POST" action="Checkout.do" onSubmit=" return checkBal()" >
<input type="submit" value="Checkout"><br><br>
<a href="Reqst.html">Place More Requests</a>

<form method="POST" action="Modify.jsp" >
<%--code to display requests--%>
<input type="submit" name="action" value="Modify" >

<form method="POST" action="Remove.jsp" >
<%--code to display requests--%>
<input type="submit" name="action" value="Remove Request">


Poonam K.
Author and ninkuma
Posts: 66424
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you submit to the server and send a page to the browser, the whole page, including its Javascript is re-interpreted. There is no way to retain Javascript variable values across requests unless you persist them to the server. Frequently the session is used to hold data between requests.

You might also investigate Ajax which will let you post back to the server without a full-page reload. But bear in mind that the user can undo everything by simply hitting the refresh button.

Bottom line: all important data must be presisted to the server and sent to the browser as necessary.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!