Javascript/jquery frustration How can I???

Posted on 2014-02-27
Last Modified: 2014-02-28
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?
Question by:DMTrump
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 3
  • 2
LVL 110

Expert Comment

by:Ray Paseur
ID: 39893353
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

Author Comment

ID: 39893475
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.

Author Comment

ID: 39893479
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39893513
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!
LVL 58

Expert Comment

ID: 39893578
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

Author Comment

ID: 39893605
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

Author Comment

ID: 39893622
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?
LVL 58

Accepted Solution

Gary earned 250 total points
ID: 39893630
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

Author Comment

ID: 39893704
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.
LVL 58

Expert Comment

ID: 39893758
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.

Author Comment

ID: 39893780
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?

Author Comment

ID: 39893877
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:
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39894698
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.

Author Comment

ID: 39895085
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

Author Comment

ID: 39895442
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.

Author Closing Comment

ID: 39895484
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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question