Solved

Javascript/jquery frustration How can I???

Posted on 2014-02-27
17
290 Views
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)
    
          $("#taxpercent").load(taxpercent);            
                            // 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?
0
Comment
Question by:DMTrump
  • 11
  • 3
  • 2
17 Comments
 
LVL 108

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
0
 

Author Comment

by:DMTrump
ID: 39893475
Yes, I can direct you to the page  go to my clients site at www.triadav.com/tav 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.
0
 

Author Comment

by:DMTrump
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.
0
 

Author Comment

by:DMTrump
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!
0
 
LVL 58

Expert Comment

by:Gary
ID: 39893578
Several things wrong
$("#countylist").val()
If you want the selected value it should be
$("#countylist :selected").val()

$("#taxpercent").load(taxpercent);  
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
$("#taxpercent").html(taxpercent);  
of if it is an input box then
$("#taxpercent").val(taxpercent);  
Ditto for tax and totaldue
0
 

Author Comment

by:DMTrump
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
0
 

Author Comment

by:DMTrump
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?
0
 
LVL 58

Accepted Solution

by:
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
         $("#taxpercent").load(taxpercent);            
          $("#thetax").load(tax);
          $("#thetotalcharge").load(totaldue);
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:DMTrump
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.
0
 
LVL 58

Expert Comment

by:Gary
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.
0
 

Author Comment

by:DMTrump
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?
0
 

Author Comment

by:DMTrump
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:
          $("#thetaxratestr").load(taxratestr);
0
 
LVL 108

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.
http://validator.w3.org/check?uri=http%3A%2F%2Fwww.triadav.com%2Ftav%2Frecordings%2Fcart.php&charset=%28detect+automatically%29&doctype=Inline&group=0

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.
http://php.net/manual/en/function.error-log.php

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.
0
 

Author Comment

by:DMTrump
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
0
 

Author Comment

by:DMTrump
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.
0
 

Author Closing Comment

by:DMTrump
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.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now