If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Do you have any code at all to base this off of? I'd like to see what you have so I know exactly what you need added.

In your original post your equation would appear to be correct for the most part, thus I don't think I understand what you are needing in order to get your page working as desired. Is it that you aren't getting the right value? If that's the case you merely need to make sure you add the values as numbers (and not strings).

Again, I'm not really sure what you are asking in context. You already seem to have a working system for most of this, just not the grand total, however it's hard to tell you what to do in relation to your code, since none of that is here for reference. So if you need the grand total displayed on the page for example, we here at the forum have no idea where to display it on the page without seeing code. Or if you already have something set up to do this but it just isn't working right, we here at the forum cannot fix the code you have without seeing it.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Hi, thanks for the quick reply. Below is the code.
I was trying to add the grandtotal code to the file calculator.js with these functions which basically add calculate the square footage and the price of each item.

First things first, I have to admit, your HTML is pretty jumbled. It certainly wouldn't pass a validator as it's missing some closing tags, and other tags don't follow the 'order of operations' (ie. closing inner-most tags before closing outer-most tags). I won't really address all of the HTML issues though as that's not what this topic is about and it'd be quite a task to rebuild/reformat all of the code.

Now, for the javascript. A few quick notes: you duplicated your round_decimals() and pad_with_zeroes() functions. I also felt those two functions were awfully... bulky. I included a minified function for formatting numbers. I also removed some of the commented code.

The first thing to do would be add the HTML element to hold your grand total. Something like this will suffice:

HTML Code:

Grand Total: <span id="grandTotal"></span>

I feel like it makes more sense to be putting these values in regular text elements rather than <input> elements since they are not editable values. Of course, I guess this assumes you aren't actually wanting to submit the value with the form. But in that case I feel it's a little silly to pass the total values via form since it's possible to easily modify those values and thus your submitted data would be tainted and wrong. You should be processing order totals that actually get used on the server's side to avoid potential issues.

Now for the script, below is essentially all you need. I do feel you could optimize your calculate functions (for instance, you could set up just 1 that takes in 3 parameters, the length field, width field and the output element/field for the total), but it's not a huge deal, it would just be cleaner and more optimized code.

Each time a total is calculated, the grand total also gets calculated. This isn't the most optimal way to do it, but again your calculate functions could be optimized and merged into one, and could be built to actually check to make sure a total exist for both items before calculating the grand total. But for now, this is a working version that does the job just fine. It will strip any 'bad' characters to get the totals as numbers, then of course adds them and places them in the <span> element I gave you earlier in this post.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thank you so much for this. I just didn't copy all the html.. It is complete and coded right in my form
A question: If I add a drop down that can show up to 4 items will this code calculate totalcost3 totalcost4 etc... if I modify the function Calculateitem2(frm) { code by changing all the 2's to 3's etc?

Again, I think you'd be much better off combining your calculate functions into one. Having 4 separate functions to calculate 4 items is a bad practice, especially if these items all use the same equation/algorithm for their calculation. You end up with a bunch of repeated code (and only 1 or 2 things changing per function).

If you are wanting to dynamically change the number of items being calculated, it would be even more beneficial to change how things are set up. Bascially, everything right now is static. Each calculation is coded for one specific thing/item and thus the grand total is coded for the 2 specific items you have right now. I don't know if you have any sort of deadlines or schedules to adhere to with this but I can certainly take a look later today and a unified calculate and grand total function that would calculate any number of items.

And as for the labeling issue of the grand total, as I don't know what the content of the email looks like I can't really tell you what you should or shouldn't do to get a proper label showing up. If the grand total itself is showing up then I suppose you could always just modify my _CalculateTotal() function to write "Grand Total: " in the <span> innerHTML as well (and just remove the text label from the page. But by default have this string in the <span> so the area is not blank to begin with).

Hello, I used the code you posted above but would be OPEN and THANKFUL FOR A MORE EFFECTIVE APPROACH.

I think the shipping cost of $25 should only be applied to item 1 unless my requirements change.
If it is easy the code could show the option to add more items if a button is pressed. In most cases someone will only order 1 item but need the ability to add more.

As for the grandtotal coming through in my php email script.
Example of what I mean here is I have input for customer to fill in company name (html form)
<input name="company">

in my php form calls on the field name
$company = $_POST['company']; and the value will come through in the received email.

maybe the grandTotal will work the same, I will test it out. (from id)

One more thing.. sorry for all the posts.. I noticed that if only 1 product is entered the grandTotal doesn't calculate until item 2 is identified. There might not be an item 2 so it needs to calculate no matter how many items.

As far as the grand total not calculating until a second item is entered, this would mainly be because there is no default value for these items. Thus no total (an invalid total actually) and so the function can't display the total. If the length and width fields had a default value of "0" the grand total should be calculated regardless of how many items were entered.

But in all fairness, it's more the overall method for all calculations that needs to be adjusted for a proper result. Having one function that dynamically calculates a total for any item will resolve that issue (and others).

As for getting the grand total in your email, because you are checking $_POST variables you will never see the grand total. $_POST variables are set from form input values. Thus the only way to see values in $_POST (AJAX aside) is having the value in an <input> with a valid name property set (id is not used in $_POST).

I do feel I need to refer back to something I mentioned earlier when I mentioned putting the grand total in a <span> and not an <input>. It's a bad idea and practice to pass total values like your grand total via a form <input> when you are already passing all of the values necessary to calculate the total in PHP. Especially if you are dealing with an order system that deals with real live orders (and money), you shouldn't be using Javascript, a client-side language, to handle the official order totaling for your records. I'm not saying it's something that could often be a problem but it's an extremely simple task to edit the input values and submit the form. For instance I could enter my typical order information then press the magical F12 button (or right-click and 'Inspect Element') and then edit the value of the grand total. When I submit you could get a grand total of $0.01 which would be absolutely incorrect. You'd receive the email with the invalid info and if the order info is sent anywhere else (a database for instance) the info would be incorrect there as well. Sure, you could just look at the other order details and actually calculate the real grand total yourself, but why put yourself through all of the extra trouble? You should really only be passing the base information to PHP and then actually calculate the total in PHP before sending an email and saving the order. And don't get me wrong, you should still be calculating this on the front-end with Javascript to display the value to the user, but that value shouldn't be your 'final' total that you immediately save and send off because it's less likely to be correct all of the time than having the server (PHP) do the calculations instead.

That's just one case scenario as well. Overall you could run into other issues with values not always being passed correctly, javascript errors, disabled javascript, etc. And without PHP there to do the actual calculations you will be getting invalid orders and totals.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Ok.. if you could come up with a function that would calculate the totals regardless of what is selected that would be great - this would resolve the number of items ordered and always show a total.. or suggest another method.

As for the post method.. I have taken an existing form that has a post method .. and customizing based on requirements. I would rather use a wordpress plugin but I need the square feet measured and values for the tooling as well as the image mouseovers as seen here - http://www.webcrawldesigns.com/dct/order-form-test/

Below I have what should be a unified function that will calculate the total of any particular item, as well as a slightly updated grand total function.

I do want to note that this is sort of a 'hack job', which is something I hate to do, but if I'm being honest, a more proper solution would involve numerous changes to the rest of your HTML, which is something I wasn't looking to get into at the moment.

This code should work, but to be fair it hasn't been tested. I should also note that I'm using document.forms[1] which is only done as your actual page seems to have another form on it, making the order form the second form (1 instead of 0). If the order form were the only form on the page it should be using document.forms[0] instead. And again as I mentioned earlier, a more proper way to do this would be to assign a 'name' attribute to the <form> tag and instead use that (document.forms["formName"]).

In order to use this new version of the function you will also have to change how you call the function. So for your first item you'd have something like

HTML Code:

<input class="short" onchange="Calculate(0)" name="width">

And for the second item you'd use

HTML Code:

<input class="short" onchange="Calculate(1)" name="width">

And so on for each new item (the same scheme applies to the length.

Lastly, I'll talk about the extra variables I added at the top. $itemCount could be set dynmaically, so that when you say, change a drop down to choose the number of items you want, this number would update. It's currently used to calculate the grand total (so that it loops through all available items). The other 3 variables are arrays that hold field names. There was probably a 'better' way to go about this, but I found it to be a simple solution for your specific case. Those arrays would need to be updated if you added more than 4 items for instance, or if the 'name' attribute was different than what I have listed there.

"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"