Javascript/jquery frustration How can I???

I've beat my head agains a wall for two days now trying to figure out why I can't get the following code to work.  This is a change action on a  <select> input and in its barest form it works.

The following code I KNOW is not correct but I don't know how to make it right:

 $("#ziplist").change(function() {
          var shipping= <?php echo $shipping; ?>;   
                     // (needs to receive a php variable called $shipping but this doesn't work)
          var subtotal= <?php echo $subtotal; ?>;  
                    // (needs to receive a php variable called $subtotal but this doesn't work)
          var taxrate="gettax.php?countynum=" + $("#countylist").val()+"&city_x=" + $("#citylist").val()+"&zip5=" + $("#ziplist").val();   
                            // the above returns a float from a MySQL table
          var taxpercent=taxrate*100;               
                              // to be displayed as 7% or 6.3% etc. for instance
          var tax=taxrate*subtotal;                 
                              // to be displayed as $6.50, for instance
          var totaldue=subtotal+shipping+tax;       
                             // to be displayed as $ value ($54.60, for instance)
                            // displays corrctly when I assign taxrate as a test value
          $("#thetax").load(tax);                                // ditto
          $("#thetotalcharge").load(totaldue);          // ditto

Open in new window

Even when I try to assign test values to shipping and tax it doesn't display
I've tried so many variations of the code above that I've gotten a headache.
can anyone help me out?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
It would be interesting to see this script in a web page, so we can see what the variables contain after the web page is rendered.  The client/server model may be an issue here, since the PHP script runs on the server, creates the HTML document (including the JavaScript) and sends this document to the client browser.  Then PHP goes back to sleep and nothing more happens until there is another request.

Is this on a test page somewhere with a public-facing URL?  If not, can you please put it up somewhere so we can look at the rendered HTML and JavaScript?  Thanks, ~Ray
DMTrumpAuthor Commented:
Yes, I can direct you to the page  go to my clients site at and click on the first item Event Recordings.  On the page you then go to, select one or more recordings to place into the cart  (return to recordings to add more) then in the cart, once it has some content, click on the Checkout button.

You'll see a list of the recording you've selected and below that a form to enter your shipping information.

You don't need to populate the name and address information, but select any county from the county drop down, then any town offered in the resulting city list then a zip code from the zipcode list.  That last action triggers the ziplist change function.  

The Shipping variable and the subtotal variable are calculated when this page loads so AFAIK should be available as soon as the document completes.  

The ziplist.change function is in a script holder in the Head section of the page.  The table of items in the cart is populated from a php mysql routine near the top of the page.

If nothing leaps out at you as being bad about my code then I'll create a stub page to test this routine so that you can diagnose it.
DMTrumpAuthor Commented:
Another variation that I tried (that didn't work either) was to have a small section of script just after the php/mysql that assigned the values for shipping and subtotal to js globals then tried to use them in the ajax function - no joy.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DMTrumpAuthor Commented:
Let's suspend this question while I create a stub that demonstrates the problem.  The page that contains this code is quite complex (everything else works) and is on a client site where I can't really give you access.  I'll build a stub on one of my spare urls where you can view everything.

Might be several hours - it's almost dinner time and my wife gets testy if I don't come to dinner!
Several things wrong
If you want the selected value it should be
$("#countylist :selected").val()

taxpercent is a number so I don't see how it can be working - .load loads an html,php etc page not a number
If you are trying to set the value/html of the #taxpercent element it should be
of if it is an input box then
Ditto for tax and totaldue
DMTrumpAuthor Commented:
The call to gettax.php works fine as is.  But what your saying indicates to me that taxrate is a string rather than a number

I had tried
var taxfloat = parseFloat(taxrate);  but that still didn't seem to work
DMTrumpAuthor Commented:
If the return from gettax.php is emitted as a string then it will display using the .load operator?  Is that right?
If the return from gettax.php is a number then it wouldn't display using .load - is that what you are saying?
taxrate is a URL in the string - you are not doing anything with it.
         var taxrate="gettax.php?countynum=" + $("#countylist").val()...

Here you are trying to multiple the taxrate (which is the URL string above) by subtotal - not gonna happen
         var taxpercent=taxrate*100;              

Doing the same again here.
         var tax=taxrate*subtotal;                

          var totaldue=subtotal+shipping+tax;      

Here you are trying to load a page on your server but taxpercent, tax and totaldue are all numbers

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DMTrumpAuthor Commented:
OK, nice of you to tell me what doesn't work. I knew already that it didn't work (lol)  How about telling me how to turn taxrate into a numeric value I can work with?  It is returned from gettax as a string like 0.07 evidently, but neither +(taxrate) nor parseFloat(taxrate) turns that into a value I can calculate with.
I don't know because I don't what your code is supposed to be doing - there is no logic to it.
Maybe if you broke your code down telling us what you expect each line to be doing
taxrate into a numeric value
Is it supposed to be a numeric value, I don't know, you have it as url at the moment. etc.
DMTrumpAuthor Commented:
OK, I've got everything working EXCEPT being able to interpret the return for the call to gettax.php as a number

I'm now accepting the output of gettax into a variable taxratestr
Then I want to convert it to a float value taxrate to do my calculations.

if I fake the conversion and use var taxrate = +('0.07'); then I geta proper float valueinto taxrate and everything else works because I'm now using .html instead of .load per your suggestion (thanks)

However if I try var taxrate = +(taxratestr); instead then no joy.  
Same with the var taxrate = parseFloat(taxratestr);

gettax, by the way is returning a float value looked up from a MySQL table but the output from that call is neither a proper string nor a number according to Javascript since I can't use it as is nor can I convert it  You call it a "url" so what can I do with it to be able to treat it as a float?
DMTrumpAuthor Commented:
This line executes the gettax.php file which retrieves a taxrate as a varchar from a MySQL table:
 var taxratestr="gettax.php?countynum=" + $("#countylist").val()+"&city_x=" + $("#citylist").val()+"&zip5=" + $("#ziplist").val();  

You call it a URL, but it IS executing and returning the proper value which I can display in a <p> of id='thetaxratestr' by using the AJAX line:
Ray PaseurCommented:
Trying to follow your directions at this link, I found an HTML document with a doctype declaration inside the menu div.  Whatever else may be wrong, this certainly needs to be addressed.

There are also </body> and </html> tags inside the document.  I can't begin to guess how this would affect the work of JavaScript.

One thought you might use, going forward, is the PHP error log.  You can timestamp and track events on the server, so it will enable you to follow the behaviors of the AJAX calls accurately.

A low cost online e-commerce solution might be available by using eBay or Amazon.  I see lots of stores there that seem to be successful, and all the "heavy lifting" of the e-commerce model has already been done.
DMTrumpAuthor Commented:
Thanks Ray,  Most of the errors that were reported came from a stray bit of code I had pasted into that page (and several others, elsewhere that supposedly returned the URL of the current page.  Useless, actually and as you saw, actually damaging.  And thanks for the reminder, I had not gotten around to doing a validation test and that's the FIRST thing you should do when you encounter problems.

I've corrected that page - eliminating that section - but upon re-validation my 24 errors still show up - not sure what's up with that - the error report now points to lines that are no longer on the page, but I'll keep trying.

And as for your suggestion that I use a ready made cart - sorry - no ready-made cart would handle this clients needs.  And the difficult part of this task has been coding a checkout page that correctly calculates the proper tax in Iowa with it's thousands of different taxing districts based on county (99 of them) city (2000+ of them) in that county and zipcode in that city where a city or rural area can be in more than one county and have several zipcodes that have different tax rates depending on whether they are inside city limits or not and which county they are in.

Final checkout will be through PayPal, by the way
DMTrumpAuthor Commented:
I have solved my problem - but in a way that has nothing to do with this question.  Thanks to Cathal who pointed out that what I was trying to do wouldn't work I took a completely different path (a much simpler one, by the way)   See my response to Ray Paseur for a little bit more information on my requirements.  

Anyway, I'm closing this question and awarding points to both Cathal and Paseur because they led me to find a better way of doing things.
DMTrumpAuthor Commented:
I'd still like to find a way to treat the return from my call to gettax.php as a string that can be converted to a float - but I don't need that anymore.  

Now, when I populate the zip ziplist <select> object in a previous ajax call I 've already narrowed down the zipcodes in such a way that I can set the value of each zipcode in the dropdown <select> to the taxrate (as a string) that I need to receive.  Thus the call var taxratestr="gettax.php?... call is replaced by var taxratestr=$(#ziplist").val; and that returns the tax as a string that I was trying to get from gettax.php.  Convert that to a float with the + operator and I've got what I need.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.