1 Replies - 1429 Views - Last Post: 01 October 2012 - 03:22 PM

#1 justin_k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-May 12

Javascript or pass data to Business Object?

Posted 01 October 2012 - 01:22 PM

Hi guys, I'm a student doing a project for my capstone programming class and I've run into a bit of a problem. The situation is I have a JSP page (services.jsp) that contains a checkbox and what I want it to do is to calculate the price of all checked items after the form. The project specs don't require transaction saving so I don't think this is a problem as long as I can carry the processed total onto another JSP or onto a Servlet. But anyway, I was wondering if Javascript is the way to go here or if I should somehow save the information into an object and useBean on a jsp to extract it. If Javascript is the consensus I'll move this topic to Javascript to get some tips on how to go about the problem. Here is the code, warning, it contains unfinished JS.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <link rel="stylesheet" type="text/css" href="main.css" />
       function calculateTotal(){
       var total;
        var theForm = document.forms["optionform"]; {
        var selectedServices = theFrom.elements["selectedServices"];
        $(document).ready(function() {
        $('input[type="checkbox"]').live('change', function() {
        calculateTotal(); //do it for initial price
      <div id="container">
	<div id="header">
	  <p>Motor City Repair Services Menu</p>
	</div><!--end header-->
           <div id="optionform">
	     <p align="center">Choose Services From the Menu:</p>
                <span><input type="checkbox" name="Oil and Filter Change">Oil and Filter Change: $21.00</span><br />
                <span><input type="checkbox" name="Mini-Service">Mini-Service: $35.00</span><br />
                <span><input type="checkbox" name="Tire Rotation and Balance">Tire Rotation and Balance: $45.00</span><br />    
                <span><input type="checkbox" name="Major Service 4 cyl">Major Service 4 cyl: Parts + Labor</span><br />
                <span><input type="checkbox" name="Minor Service 4 cyl">Minor Service 4 cyl: Parts + Labor</span><br />
                <span><input type="checkbox" name="Major Service 6 cyl">Major Service 6 cyl: Parts + Labor</span><br />
                <span><input type="checkbox" name="Minor Service 6 cyl">Minor Service 6 cyl: Parts + Labor</span><br />
                <span><input type="checkbox" name="Break Service">Break Service: $50.00</span><br />
                <span><input type="checkbox" name="Standard Car Wash">Standard Car Wash: $20.00</span><br />
                <span><input type="checkbox" name="Car Detailing">Car Detailing: $90.00</span><br />
                <span><input type="checkbox" name="Engine Change Out">Engine Change Out: Parts + Labor</span><br />
                <span><input type="submit" name="submit" value="Submit" /></span>
                Total: <span class="total"></span>
	    </div><!--end optionform-->
	<div id="footer">
	 <p>Developed by Quality Code</p>
  </div><!--end container-->

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript or pass data to Business Object?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1158
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Javascript or pass data to Business Object?

Posted 01 October 2012 - 03:22 PM

Javascript is a browser scripting language, it's never had ability to persist data, although there are some options available now. But even if you could persist your transaction via the user's file system, that's a ridiculous idea.

I see you're using Javascript to do the computation. I guess in an attempt to keep it dynamic, but obviously the problem is that the computation won't be available across the next page request. Two solutions: (1) have the a Servlet or JSP grab the form data, crunch the numbers, save the answer in a bean, session variable, or Servlet object, and respond to the user; and (2) Use AJAX to do (1) while still allowing the HTML to be dynamic (no page reloading). Either way you can't escape using server-side scripts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1