Javascript Problem

3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 13, 2011   (RSS)

I've inherited a Javascript function that does tax calculation based on the State value entered in form a field.
function onchangestate(type){
if(type=='FL'){
tax=total * 0.06;
total=total + tax;
}

else{

total=total ;
}

$('#totalcost').html('$' + total);

}

The field code is:
<select id="state" name="state" onChange="javascript:onchangestate(this.value)">
<option value="" selected="selected">Select</option>
<option id="USA-AL" value="AL" >Alabama (AL)</option>
...more state options...
<option id="USA-FL" value="FL" >Florida (FL)</option>
<option id="USA-WY" value="WY">Wyoming (WY)</option>
...more state options...
</select>

This all works fine when the State value (FL or not FL) is selected from the list of options.

I modified the field code to pull the state value from a user record instead of entering it manually with:
<input type="text" name="state" id="state" onChange="javascript:onchangestate(this.value)" value="<?php echo htmlspecialchars(@$_REQUEST['state']); ?>" >


The state field now populates from the user record, but I'm guessing that since there are no products selected before field is populated, the function is not called at the appropriate time and I can’t get the tax calculation to work.

Can someone help?

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Javascript Problem

By Jason - December 13, 2011

Hi Jerry,

I think the issue is that the javascript function is only ever triggered when the field changes. Since you're now using a text field, unless they change that value, the function is never called. Is there a reason you change it from a select field to a text field?

You can always put a javascript call to the function directly in the page when it loads and then use PHP to put a value in for the argument.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/