Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Javascript Problem

 

 


gkornbluth
Veteran

Dec 13, 2011, 4:57 AM

Post #1 of 3 (354 views)
Shortcut
Javascript Problem Can't Post

I've inherited a Javascript function that does tax calculation based on the State value entered in form a field.

Code
function onchangestate(type){ 
if(type=='FL'){
tax=total * 0.06;
total=total + tax;
}

else{

total=total ;
}

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

}

The field code is:

Code
<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:

Code
<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


Jason
Staff / Moderator


Dec 13, 2011, 10:44 AM

Post #2 of 3 (345 views)
Shortcut
Re: [gkornbluth] Javascript Problem [In reply to] Can't Post

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 - Programmer 
interactivetools.com

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


gkornbluth
Veteran

Dec 13, 2011, 12:11 PM

Post #3 of 3 (342 views)
Shortcut
Re: [Jason] Javascript Problem [In reply to] Can't Post

I changed it because I only wanted the value that was pulled from the current user record. I'll try your approach.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com