By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

help with recordsets and user entered values

P: 22
Hello to all and thanks for your help in advance.

I am having been fighting with this problem for a couple of days now and after much unsuccessful research, I am still looking for an answer. So here it is. I have 9 hidden fields(qty12,24,36,72,144,288,576,1008,2016) on a form containing recordsets pulled from a mySQL database. The values from the database are there... (now we get to the problem.) I have a text box called quantity where a user inputs how many of a particular product they desire. if the user enters a number less than or equal to 12 it has to be multiplied by qty12 if its 13-24 it must use recordset qty24, if its between 25-36 it must use qty36 and so on all the way out to 2016. This is what I have written so far any help would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. function getCalc(){      
  2. var f = document.prodform.quantity.value;          
  3. var g = document.prodform.qty12.value;     
  4. var h = document.prodform.qty24.value;     
  5. var j = document.prodform.qty36.value;     
  6. var k = document.prodform.qty72.value;     
  7. var l = document.prodform.qty144.value;     
  8. var m = document.prodform.qty288.value;     
  9. var n = document.prodform.qty576.value;     
  10. var o = document.prodform.qty1008.value;     
  11. var p = document.prodform.qty2016.value;         
  12. if(f >= 12){                 
  13. Number(f * g);         
  14. }else if
  15. (f < 24){               
  16.  Number(f * h);         
  17. }else if
  18. (f < 36){                 
  19. Number(f * j);         
  20. }else if(f < 72){                 
  21. Number(f * k);         
  22. }else if(f < 144){                 
  23. Number(f * l);         
  24. }else if(f < 288){                 
  25. Number(f * m);         
  26. }else if(f < 576){                 
  27. Number(f * n);          
  28. }else if(f < 1008){                 
  29. Number(f * o);         
  30. }else if(f < 2016){                 
  31. Number(f * p);         
  32. }            
  33.  
  34. }     
  35.  
  36. alert (document.prodform.price.value);     
The alert is only there so that I could make sure that everything worked. it will be removed and the value will be placed in a text box.

I am sure that there is a much easier way to do, any suggestions or places to go for research would be great! Again thank you all.
Aug 4 '08 #1
Share this Question
Share on Google+
32 Replies


acoder
Expert Mod 15k+
P: 16,027
You need to use parseInt to convert a string into an integer.

On line 12, it should be <=, not >=. Also, it's always a good idea to use meaningful variable names.

You could probably shorten the code using arrays, but in this case, it's just about manageable, so should be OK with the if-statements.
Aug 4 '08 #2

P: 22
acoder thanks for the speedy reply. i am new javascript programming and i am not completely sure what you mean. the parseint functions converts which string? the user entered quantity? or the recordset? about the meaningful variables i get what you're saying. i will be changing those now.



You need to use parseInt to convert a string into an integer.

On line 12, it should be <=, not >=. Also, it's always a good idea to use meaningful variable names.

You could probably shorten the code using arrays, but in this case, it's just about manageable, so should be OK with the if-statements.
Aug 4 '08 #3

acoder
Expert Mod 15k+
P: 16,027
The user entered quantity, e.g.
Expand|Select|Wrap|Line Numbers
  1. function getCalc(){      
  2. var quantity = parseInt(document.prodform.quantity.value);
  3. //...
  4. if (quantity <= 12) {
  5. price = quantity * qty12;
  6. } else if...
Aug 4 '08 #4

P: 22
acoder this is what i did with what you sent me... unfortunately, it doesnt work in IE or FF (havent even bothered to check safari) any ideas????

thanks again for all the help. Maybe it's because the values from the recordsets have decimals?? also i need to be able to show these as currency values and not just integers...


Expand|Select|Wrap|Line Numbers
  1.  
  2. function getCalc(){          
  3. var qty = parseInt(document.prodform.quantity.value);          
  4. var qty12=document.prodform.qty12.value;     
  5. var qty24=document.prodform.qty24.value;     
  6. var qty36=document.prodform.qty36.value;     
  7. var qty72=document.prodform.qty72.value;     
  8. var qty144=document.prodform.qty144.value;     
  9. var qty288=document.prodform.qty288.value;     
  10. var qty576=document.prodform.qty576.value;     
  11. var qty1008=document.prodform.qty1008.value;     
  12. var qty2016=document.prodform.qty2016.value;          
  13. var price = document.prodform.price.value;          
  14. if(qty >= 12){                                 
  15. price = Number(qty * qty12);                         
  16. }else if                     
  17. (qty <= 24){                                
  18. price = Number(qty * qty24);                         
  19. }else if                     
  20. (qty <= 36){                                 
  21. price = Number(qty * qty36);                         
  22. }else if                     
  23. (qty <= 72){                                 
  24. price = Number(qty * qty72);                         
  25. }else if                     
  26. (qty <= 144){                                 
  27. price = Number(qty * qty144);                         
  28. }else if                     
  29. (qty <= 288){                                 
  30. price = Number(qty * qty288);                         
  31. }else if                     
  32. (qty <= 576){                                 
  33. price = Number(qty * qty576);                         
  34.  }else if                     
  35. (qty <= 1008){                                 
  36. price = Number(qty * qty1008);                         
  37. }else if                     
  38. (qty <= 2016){                                 
  39. price = qty * Number(qty2016);                                  alert(document.prodform.price.value);                                                                                  }                                    
  40.  

The user entered quantity, e.g.
Expand|Select|Wrap|Line Numbers
  1. function getCalc(){      
  2. var quantity = parseInt(document.prodform.quantity.value);
  3. //...
  4. if (quantity <= 12) {
  5. price = quantity * qty12;
  6. } else if...
Aug 4 '08 #5

acoder
Expert Mod 15k+
P: 16,027
If it's decimals, then you need to use parseFloat.

Since the hidden fields are being generated using server-side code, why not use similar code to generate the JavaScript variables, e.g.
Expand|Select|Wrap|Line Numbers
  1. var qty12 = <?php echo $val; ?>;
There's also no need to use Number syntax. The two variables will be numbers which you can multiply together.
Aug 5 '08 #6

P: 22
OMG!!! this is getting really frustrating... here's the new code that also doesn't work. I understood what you meant about the parse float but since you said to use it on the user entered value, there's not a need for it as the user can only enter numbers that arent decimals..... IDK what is going on with this but i am really at a road block. any other ideas?????


Expand|Select|Wrap|Line Numbers
  1. function getCalc(){          
  2. var qty = parseInt(document.prodform.quantity.value);          
  3. var qty12="<?php echo $row_Recordset1['qty12']; ?>"; 
  4. var qty24="<?php echo $row_Recordset2['qty24']; ?>";     
  5. var qty36="<?php echo $row_Recordset3['qty36']; ?>"; 
  6. var qty72="<?php echo $row_Recordset4['qty72']; ?>"; 
  7. var qty144="<?php echo $row_Recordset5['qty144']; ?>";     
  8. var qty288="<?php echo $row_Recordset6['qty288']; ?>";     
  9. var qty576="<?php echo $row_Recordset7['qty576']; ?>";     
  10. var qty1008="<?php echo $row_Recordset8['qty1008']; ?>";     
  11. var qty2016="<?php echo $row_Recordset9['qty2016']; ?>";          
  12. var price = document.prodform.price.value;          
  13.  
  14. if(qty >= 12){                                 
  15.         price = (qty * qty12);                         
  16.  
  17.                               }else if                     
  18. (qty <= 24){                                
  19.         price = (qty * qty24);                         
  20.                               }else if                     
  21. (qty <= 36){                                 
  22.         price = (qty * qty36);                         
  23.                              }else if                     
  24. (qty <= 72){                                 
  25.         price = (qty * qty72);                         
  26.                              }else if                     
  27. (qty <= 144){                                 
  28.         price = (qty * qty144);                         
  29.                              }else if                     
  30. (qty <= 288){                                  
  31.         price = (qty * qty288);                         
  32.                              }else if                     
  33. (qty <= 576){                                 
  34.         price = (qty * qty576);                          
  35.                              }else if                     
  36. (qty <= 1008){                                 
  37.         price = (qty * qty1008);                         
  38.                              }else if                     
  39. (qty <= 2016){                                 
  40.         price = (qty * qty2016); 
  41.                                  alert(document.prodform.price.value);                                                                                  }                                    
  42.  
  43.  
  44.  
If it's decimals, then you need to use parseFloat.

Since the hidden fields are being generated using server-side code, why not use similar code to generate the JavaScript variables, e.g.
Expand|Select|Wrap|Line Numbers
  1. var qty12 = <?php echo $val; ?>;
There's also no need to use Number syntax. The two variables will be numbers which you can multiply together.
Aug 5 '08 #7

acoder
Expert Mod 15k+
P: 16,027
qty12, qty24, etc. should be floats, not strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. var qty12=<?php echo $row_Recordset1['qty12']; ?>; 
  2. var qty24=<?php echo $row_Recordset2['qty24']; ?>;
  3. // and so on...
  4.  
Why have you got qty >= 12? That would always multiply by the qty12 value. Surely, you mean (qty <= 12)?
Aug 5 '08 #8

P: 22
acoder, have a question for ya... since i have those php variables being passed to hidden text boxes on the form, (and they actually show up) is there a way i can reference them instead of trying to call the php variable into the Javascriptfunction? i dont very much about php (still tryin to learn while i am doing this....) so i am trying to find the most streamlined method of making the client side do most of the work...


qty12, qty24, etc. should be floats, not strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. var qty12=<?php echo $row_Recordset1['qty12']; ?>; 
  2. var qty24=<?php echo $row_Recordset2['qty24']; ?>;
  3. // and so on...
  4.  
Why have you got qty >= 12? That would always multiply by the qty12 value. Surely, you mean (qty <= 12)?
Aug 5 '08 #9

P: 22
ARGGHHHH!!!! this doesnt work either?!?!?!?!? what i am doing wrong here?????


Expand|Select|Wrap|Line Numbers
  1. function getCalc(){          
  2. var qty = parseInt("document.prodform.quantity.value",10);     
  3.  
  4. alert(document.prodform.price.value);     
  5.  
  6. var qty12 = parseFloat(<?php echo $row_Recordset1['qty12']; ?>);     
  7. var qty24 = parseFloat(<?php echo $row_Recordset2['qty24']; ?>);     
  8. var qty36 = parseFloat(<?php echo $row_Recordset3['qty36']; ?>);     
  9. var qty72 = parseFloat(<?php echo $row_Recordset4['qty72']; ?>);     
  10. var qty144 = parseFloat(<?php echo $row_Recordset5['qty144']; ?>);     
  11. var qty288 = parseFloat(<?php echo $row_Recordset6['qty288']; ?>);     
  12. var qty576 = parseFloat(<?php echo $row_Recordset7['qty576']; ?>);     
  13. var qty1008 = parseFloat(<?php echo $row_Recordset8['qty1008']; ?>);     
  14. var qty2016 = parseFloat(<?php echo $row_Recordset9['qty2016']; ?>);          
  15.  
  16. var price = document.prodform.price.value;               
  17. if(qty <= 12){                                 
  18. price = (qty * qty12);                         
  19. }else if                     
  20. (qty <= 24){                                
  21. price = (qty * qty24);                         
  22. }else if                     
  23. (qty <= 36){                                 
  24. price = (qty * qty36);                         
  25. }else if                     
  26. (qty <= 72){                                 
  27. price = (qty * qty72);                         
  28. }else if                     
  29. (qty <= 144){                                 
  30. price = (qty * qty144);                        
  31. }else if                     
  32. (qty <= 288){                                 
  33. price = (qty * qty288);                         
  34. }else if                     
  35. (qty <= 576){                                 
  36. price = (qty * qty576);                          
  37. }else if                     
  38. (qty <= 1008){                                 
  39. price = (qty * qty1008);                         
  40. }else if                     
  41. (qty <= 2016){                                 
  42. price = (qty * qty2016);                                                                                                                    }                                    
  43. }       
  44.  
  45.  
  46.  
Aug 5 '08 #10

acoder
Expert Mod 15k+
P: 16,027
acoder, have a question for ya... since i have those php variables being passed to hidden text boxes on the form, (and they actually show up) is there a way i can reference them instead of trying to call the php variable into the Javascriptfunction?
Of course! As an example,
Expand|Select|Wrap|Line Numbers
  1. var qty12 = parseFloat(document.prodform.qty12.value);
Aug 5 '08 #11

acoder
Expert Mod 15k+
P: 16,027
ARGGHHHH!!!! this doesnt work either?!?!?!?!? what i am doing wrong here?????
How are you calling getCalc()? What result were you expecting and what are you getting?
Aug 5 '08 #12

P: 22
ok heres the source for the entire page. basically all my other functions work with the exception of the one to calculate quantity * price.

the code is supposed to do this:

user --> enters a quantity of 150-->getCalc says number is greater than 144 use the next one up = qty288--> cust(150) * qty288 --> spits out answer in text box for price. once that value is placed in the text box i need you use to later help add up the grand total.

price + setupchrg = grand total

price = function getCalc()
setup = function getSetup() ( this works in IE & FF) I took this out as it is 4 divs with drops downs that are just way too long to fill in here.

grandtot = function getgrandTotal()(this works in IE & FF)






Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <?php require_once('mySQlConn.php'); ?> 
  3. <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")  {   $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);    switch ($theType) {     case "text":       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";       break;         case "long":     case "int":       $theValue = ($theValue != "") ? intval($theValue) : "NULL";       break;     case "double":       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";       break;     case "date":       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";       break;     case "defined":       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;       break;   }   return $theValue; } }  mysql_select_db($database_mySQlConn, $mySQlConn); 
  4.  
  5. $query_Recordset1 = "SELECT qty12 FROM price_breakdown  WHERE productID = '4401'"; $Recordset1 = mysql_query($query_Recordset1, $mySQlConn) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1);   mysql_select_db($database_mySQlConn, $mySQlConn); 
  6.  
  7. $query_Recordset2 = "SELECT qty24 FROM price_breakdown WHERE productID='4401'"; $Recordset2 = mysql_query($query_Recordset2, $mySQlConn) or die(mysql_error()); $row_Recordset2 = mysql_fetch_assoc($Recordset2); $totalRows_Recordset2 = mysql_num_rows($Recordset2);  mysql_select_db($database_mySQlConn, $mySQlConn); 
  8.  
  9. $query_Recordset3 = "SELECT qty36 FROM price_breakdown WHERE productID='4401'"; $Recordset3 = mysql_query($query_Recordset3, $mySQlConn) or die(mysql_error()); $row_Recordset3 = mysql_fetch_assoc($Recordset3); $totalRows_Recordset3 = mysql_num_rows($Recordset3);   
  10.  
  11. $query_Recordset4 = "SELECT qty72 FROM price_breakdown  WHERE productID = '4401'"; $Recordset4 = mysql_query($query_Recordset4, $mySQlConn) or die(mysql_error()); $row_Recordset4 = mysql_fetch_assoc($Recordset4); $totalRows_Recordset4 = mysql_num_rows($Recordset4);  
  12.  
  13. $query_Recordset5 = "SELECT qty144 FROM price_breakdown  WHERE productID = '4401'"; $Recordset5 = mysql_query($query_Recordset5, $mySQlConn) or die(mysql_error()); $row_Recordset5 = mysql_fetch_assoc($Recordset5); $totalRows_Recordset5 = mysql_num_rows($Recordset5);  
  14.  
  15. $query_Recordset6 = "SELECT qty288 FROM price_breakdown  WHERE productID = '4401'"; $Recordset6 = mysql_query($query_Recordset6, $mySQlConn) or die(mysql_error()); $row_Recordset6 = mysql_fetch_assoc($Recordset6); $totalRows_Recordset6 = mysql_num_rows($Recordset6);  
  16.  
  17. $query_Recordset7 = "SELECT qty576 FROM price_breakdown  WHERE productID = '4401'"; $Recordset7 = mysql_query($query_Recordset7, $mySQlConn) or die(mysql_error()); $row_Recordset7 = mysql_fetch_assoc($Recordset7); $totalRows_Recordset7 = mysql_num_rows($Recordset7);  
  18.  
  19. $query_Recordset8 = "SELECT qty1008 FROM price_breakdown  WHERE productID = '4401'"; $Recordset8 = mysql_query($query_Recordset8, $mySQlConn) or die(mysql_error()); $row_Recordset8 = mysql_fetch_assoc($Recordset8); $totalRows_Recordset8 = mysql_num_rows($Recordset8);  
  20.  
  21. $query_Recordset9 = "SELECT qty2016 FROM price_breakdown  WHERE productID = '4401'"; $Recordset9 = mysql_query($query_Recordset9, $mySQlConn) or die(mysql_error()); $row_Recordset9 = mysql_fetch_assoc($Recordset9); $totalRows_Recordset9 = mysql_num_rows($Recordset9); ?> 
  22.  
  23.  
  24. <style type="text/css"> 
  25. <!-- #apDiv1 {     position:absolute;     width:349px;     height:225px;     z-index:1;     left: 456px;     top: 86px; } 
  26.  
  27. #apDiv2 {     position:absolute;     width:275px;     height:229px;     z-index:2;     left: 12px;     top: 80px; } 
  28.  
  29. #apDiv3 {     position:absolute;     width:207px;     height:236px;     z-index:3;     left: 16px;     top: 81px; } --> 
  30.  
  31. </style>  
  32.  
  33. <style type="text/css"> 
  34. <!-- #apDiv1 {     position:absolute;     width:200px;     height:115px;     z-index:1;     left: 11px;     top: 131px;     visibility: hidden; }  
  35.  
  36. #apDiv2 {     position:absolute;     width:200px;     height:115px;     z-index:1;     left: 11px;     top: 163px;     visibility: hidden; } 
  37.  
  38. #apDiv3 {     position:absolute;     width:200px;     height:115px;     z-index:1;     left: 11px;     top: 196px;     visibility: hidden; } 
  39.  
  40. #apDiv4 {     position:absolute;     width:200px;     height:115px;     z-index:1;     left: 11px;     top: 229px;     visibility: hidden; } 
  41.  
  42. #apDiv5 {     position:absolute;     width:302px;     height:216px;     z-index:2;     left: 251px;     top: 20px; } 
  43.  
  44. #apDiv6 {     position:absolute;     width:316px;     height:382px;     z-index:3;     left: 345px;     top: 39px; }  --> 
  45.  
  46. </style>  
  47.  
  48. <script language ="javascript" type="text/javascript">  
  49.  
  50. function getCalc(){          
  51. var qty = parseInt(document.prodform.quantity.value);               
  52. var qty12 = parseFloat(<?php echo $row_Recordset1['qty12']; ?>);     
  53. var qty24 = parseFloat(<?php echo $row_Recordset2['qty24']; ?>);     
  54. var qty36 = parseFloat(<?php echo $row_Recordset3['qty36']; ?>);     
  55. var qty72 = parseFloat(<?php echo $row_Recordset4['qty72']; ?>);     
  56. var qty144 = parseFloat(<?php echo $row_Recordset5['qty144']; ?>);     
  57. var qty288 = parseFloat(<?php echo $row_Recordset6['qty288']; ?>);     
  58. var qty576 = parseFloat(<?php echo $row_Recordset7['qty576']; ?>);     
  59. var qty1008 = parseFloat(<?php echo $row_Recordset8['qty1008']; ?>);     
  60. var qty2016 = parseFloat(<?php echo $row_Recordset9['qty2016'];
  61. ?>);                         
  62. var price = document.prodform.price.value;                    
  63. if(qty <= 12){                                 
  64. price = (qty * qty12);                                                 
  65. }else if{                     
  66. (qty <= 24)                                
  67. price = (qty * qty24);                         
  68. }else if {                     
  69. (qty <= 36)                                 
  70. price = (qty * qty36);                         
  71. }else if {                     
  72. (qty <= 72)                                 
  73. price = (qty * qty72);                         
  74. }else if {                     
  75. (qty <= 144)                                 
  76. price = (qty * qty144);                         
  77. }else if {                     
  78. (qty <= 288)                                 
  79. price = (qty * qty288);                         
  80. }else if {                     
  81. (qty <= 576)                                 
  82. price = (qty * qty576);                          
  83. }else if {                     
  84. (qty <= 1008)                                 
  85. price = (qty * qty1008);                         
  86. }else if {                     
  87. (qty <= 2016)                                 
  88. price = (qty*qty2016);                                                                                                                                                                                                   }                                    }             
  89. </script>   
  90.  
  91. <script>  function getSetup()  { 
  92.     var y = document.prodform.setupchrg.value;         
  93. y = (document.prodform.numsel.value * document.prodform.setup49.value);             document.prodform.setupchrg.value = y;        
  94. }  
  95.  
  96. function getgrandTotal()   {     
  97.  
  98. <!--  create var for prodform.price.value to be added here to complete price-->     var x = document.prodform.price.value;     
  99. var y = document.prodform.setupchrg.value     
  100. var z = Number(x + y);              
  101. document.prodform.grandtotal.value = z;                
  102. }  
  103. </script>  
  104.  
  105. <script language="javascript" type="text/javascript">  
  106.  
  107. function dropnumb()  {      
  108. var numsel = document.getElementById('numsel');          
  109. var maxind = Number(numsel.options[numsel.selectedIndex].value);               
  110. for(var x=1;x<=maxind;x++)               
  111. {                        
  112. document.getElementById("apDiv"+x).style.visibility = "visible";               }               
  113.  
  114. for(var x=x;x<=5;x++)               {                        document.getElementById("apDiv"+x).style.visibility = "hidden";           
  115. }  
  116. </script> 
  117.  
  118. </head>  
  119. <body> 
  120. <form action="#" method="get" name="prodform"> 
  121. <input name="quantity" type="text" id="quantity" onfocus="getCalc()" /> <label>Qty.</label>  <br  /> <br  />  <label>  
  122.  
  123. <select name="position" id="position">    
  124. <option value="0">select</option>    
  125. <option value="1">Front Side Only</option>    
  126. <option value="2">Back Side Only</option>    
  127. <option value="3">Both Sides</option>  
  128. </select> Position</label>  
  129. <br  /> <br  /> 
  130.  
  131. <select id="numsel" name="numsel" onchange="dropnumb(0);">   
  132. <option value="0">Blank</option>   
  133. <option value="1">1</option>   
  134. <option value="2">2</option>   
  135. <option value="3">3</option>   
  136. <option value="4">4</option> 
  137. </select>   
  138.  
  139. <br  />   
  140.  
  141. <input name="setup49" type="hidden" id="setup" value="49.00" />      
  142. <input name="qty12" type="hidden" id="qty12" value="<?php echo $row_Recordset1['qty12']; ?>" />       
  143. <br/>   
  144. <input name="qty24" type="hidden" id="qty24"  value="<?php echo $row_Recordset2['qty24']; ?>"/>     
  145. <br />     
  146. <input name="qty36" type="hidden" id="qty36" value="<?php echo $row_Recordset3['qty36']; ?>"/>     
  147. <br />   
  148. <input name="qty72" type="hidden" id="qty72" value="<?php echo $row_Recordset4['qty72']; ?>"/>     
  149. <br />   
  150. <input name="qty144" type="hidden" id="qty144" value="<?php echo $row_Recordset5['qty144']; ?>"/>     
  151. <br />   
  152. <input name="qty288" type="hidden" id="qty288" value="<?php echo $row_Recordset6['qty288']; ?>"/>     
  153. <br />  
  154.  <input name="qty576" type="hidden" id="qty576" value="<?php echo $row_Recordset7['qty576']; ?>"/>     
  155. <br />   
  156. <input name="qty1008" type="hidden" id="qty1008" value="<?php echo $row_Recordset8['qty1008']; ?>"/>     
  157. <br />   
  158. <input name="qty2016" type="hidden" id="qty2016" value="<?php echo $row_Recordset9['qty2016']; ?>"/> 
  159. <?php mysql_free_result($Recordset1); 
  160. mysql_free_result($Recordset2); 
  161. mysql_free_result($Recordset3); 
  162. mysql_free_result($Recordset4); 
  163. mysql_free_result($Recordset5); 
  164. mysql_free_result($Recordset6); 
  165. mysql_free_result($Recordset7); 
  166. mysql_free_result($Recordset8); 
  167. mysql_free_result($Recordset9); ?>     
  168. <div id="apDiv9">   
  169. <label> price</label>   <input name="price" type="text" id="price" onfocus="getCalc()"   />   
  170. <br />   
  171. <label> setup charge </label>   
  172. <input name="setupchrg" type="text" id="setupchrg" onfocus="getSetup()"  />   <br  />   
  173. <label>grand total   
  174. <input name="grandtotal" type="text" id="grandtotal" onfocus="getgrandTotal()"  />   </label>   
  175. <br /> 
  176. </div> 
  177. </form>  
  178. </body> 
  179. </html> 
  180.  
Aug 5 '08 #13

acoder
Expert Mod 15k+
P: 16,027
You've forgotten to set the value of the price field at the end of the function, i.e.
Expand|Select|Wrap|Line Numbers
  1. document.prodform.price.value = price;
Aug 5 '08 #14

P: 22
you know? when i got home last night, i thought the same thing. when i got to work this morning, i had already placed it in the code. so idk.... but it still doesnt work either.


You've forgotten to set the value of the price field at the end of the function, i.e.
Expand|Select|Wrap|Line Numbers
  1. document.prodform.price.value = price;
Aug 6 '08 #15

acoder
Expert Mod 15k+
P: 16,027
For qty12 though to qty2016, use either:
Expand|Select|Wrap|Line Numbers
  1. var qty12 = <?php echo $row_Recordset1['qty12']; ?>;
or
Expand|Select|Wrap|Line Numbers
  1. var qty24 = parseFloat(document.prodform.qty24.value);
but not a combination of the two like you currently have.
Aug 6 '08 #16

P: 22
acoder,
good news!!! i got it working but there is another problem. All the qty# fields are going to be less than 2016 (the last one in the list). so is there way i can say if qty is less than 24 BUT NO MORE than 35 use this formula?
or something similar thanks again




Expand|Select|Wrap|Line Numbers
  1.  
  2. if (qty <= 12 <24) <-- this     {         
  3. y =  qty * <?php echo $row_Recordset1['qty12']; ?>;          document.prodform.price.value = y;     
  4.                                }
  5.  
  6.  

For qty12 though to qty2016, use either:
Expand|Select|Wrap|Line Numbers
  1. var qty12 = <?php echo $row_Recordset1['qty12']; ?>;
or
Expand|Select|Wrap|Line Numbers
  1. var qty24 = parseFloat(document.prodform.qty24.value);
but not a combination of the two like you currently have.
Aug 6 '08 #17

acoder
Expert Mod 15k+
P: 16,027
so is there way i can say if qty is less than 24 BUT NO MORE than 25 use this formula?
Have you got the numbers right here? If it's less than 24, it's never going to be more than 25.
Aug 6 '08 #18

P: 22
see what i mean??? so that it doesnt interfere with the next recordset.




Expand|Select|Wrap|Line Numbers
  1. function getPrice(qty)
  2.  
  3.  
  4. {
  5.  
  6.     var qty = parseInt(document.prodform.quantity.value, 10);
  7.  
  8.  
  9.     var qty12 = <?php echo $row_Recordset1['qty12']; ?>;
  10.     var qty24 = <?php echo $row_Recordset2['qty24']; ?>;
  11.     var qty36 = <?php echo $row_Recordset3['qty36']; ?>;
  12.     var qty72 = <?php echo $row_Recordset4['qty72']; ?>;
  13.     var qty144 = <?php echo $row_Recordset5['qty144']; ?>;
  14.     var qty288 = <?php echo $row_Recordset6['qty288']; ?>;
  15.     var qty576 = <?php echo $row_Recordset7['qty576']; ?>;
  16.     var qty1008 = <?php echo $row_Recordset8['qty1008']; ?>;
  17.     var qty2016 = <?php echo $row_Recordset9['qty2016']; ?>;
  18.  
  19.     var y = document.prodform.price.value;
  20.  
  21.     if (qty >= 12 || qty>=23)     ??this one i am not sure about...
  22.     {
  23.         y =  qty * <?php echo $row_Recordset1['qty12']; ?>; 
  24.         document.prodform.price.value = y;
  25.     }
  26.         if(qty <= 24 || qty >= 35); {
  27.         y =  qty * <?php echo $row_Recordset2['qty24']; ?>; 
  28.         document.prodform.price.value = y;
  29.     }                          
  30.          if(qty <= 36 || qty >= 71); {
  31.          y =  qty * <?php echo $row_Recordset3['qty36']; ?>; 
  32.         document.prodform.price.value = y;
  33.     }       
  34.          if(qty <= 72 || qty >= 143); {
  35.         y =  qty * <?php echo $row_Recordset4['qty72']; ?>; 
  36.         document.prodform.price.value = y;
  37.     }       
  38.          if(qty <= 144 || qty >= 287); {
  39.         y =  qty * <?php echo $row_Recordset5['qty144']; ?>; 
  40.         document.prodform.price.value = y;
  41.     }               
  42.          if(qty <= 288 || qty >= 575); {
  43.         y =  qty * <?php echo $row_Recordset6['qty288']; ?>; 
  44.         document.prodform.price.value = y;
  45.     }       
  46.          if(qty <= 576 || qty >= 1007); {
  47.         y =  qty * <?php echo $row_Recordset7['qty576']; ?>; 
  48.         document.prodform.price.value = y;
  49.     }       
  50.          if(qty <= 1008 || qty >= 2015); {
  51.         y =  qty * <?php echo $row_Recordset8['qty1008']; ?>; 
  52.         document.prodform.price.value = y;
  53.     }                   
  54.          if(qty <= 2016); {
  55.         y =  qty * <?php echo $row_Recordset9['qty2016']; ?>; 
  56.         document.prodform.price.value = y;
  57.     }                                                                        
  58.                                                                                                                                                                                                } 



Have you got the numbers right here? If it's less than 24, it's never going to be more than 25.
Aug 6 '08 #19

acoder
Expert Mod 15k+
P: 16,027
Oh, I see you've changed those else-if statements into separate if statements. Either use if-else-if or if you want them separate, use something like:
Expand|Select|Wrap|Line Numbers
  1. if (qty >= 12 && qty <=23)
Aug 6 '08 #20

P: 22
this things still isnt right... i really dont know why!!!!!!

for some reason, when i enter a quantity, the only number it calculates by qty2016 value = 1.81. like it doesn't cycle thru the entire function,

Expand|Select|Wrap|Line Numbers
  1.  
  2. function getPrice()   {          
  3. var qty = parseInt(document.prodform.quantity.value, 10);               
  4. var qty12 = <?php echo $row_Recordset1['qty12']; ?>;     
  5. var qty24 = <?php echo $row_Recordset2['qty24']; ?>;     
  6. var qty36 = <?php echo $row_Recordset3['qty36']; ?>;     
  7. var qty72 = <?php echo $row_Recordset4['qty72']; ?>;     
  8. var qty144 = <?php echo $row_Recordset5['qty144']; ?>;     
  9. var qty288 = <?php echo $row_Recordset6['qty288']; ?>;     
  10. var qty576 = <?php echo $row_Recordset7['qty576']; ?>;     
  11. var qty1008 = <?php echo $row_Recordset8['qty1008']; ?>;     
  12. var qty2016 = <?php echo $row_Recordset9['qty2016']; ?>;          
  13. var y = document.prodform.price.value;                           
  14.  
  15. if (qty >= 12 && qty <= 23) {         
  16. y =  qty * <?php echo $row_Recordset1['qty12']; ?>          document.prodform.price.value =y;                                 }
  17.  
  18. else         {              
  19. if(qty <= 24 && qty >= 35) {         
  20. y =  qty * <?php echo $row_Recordset2['qty24']; ?>;          document.prodform.price.value = y;                                                        }                            
  21. else         {                                                      
  22. if(qty <= 36 && qty >= 71) {          
  23. y =  qty * <?php echo $row_Recordset3['qty36']; ?>;         document.prodform.price.value = y;                                                     }                     
  24. else         {                       
  25. if(qty <= 72 && qty >= 143) {         
  26. y =  qty * <?php echo $row_Recordset4['qty72']; ?>;          document.prodform.price.value = y;                                                      }                      
  27. else         
  28. {               
  29. if(qty <= 144 && qty >= 287) {         
  30. y =  qty * <?php echo $row_Recordset5['qty144']; ?>;          document.prodform.price.value = y;                                                       }                          
  31. else         
  32. {                  
  33. if(qty <= 288 && qty >= 575) {         
  34. y =  qty * <?php echo $row_Recordset6['qty288']; ?>;          document.prodform.price.value = y;                                                       }                      
  35. else         
  36. {              
  37. if(qty <= 576 && qty >= 1007) {         
  38. y =  qty * <?php echo $row_Recordset7['qty576']; ?>;          document.prodform.price.value = y;                                                         }                      
  39. else         
  40. {              
  41. if(qty <= 1008 && qty >= 2015) {         
  42. y =  qty * <?php echo $row_Recordset8['qty1008']; ?>;          document.prodform.price.value = y;                                                         }                      
  43. else         .
  44. {                              
  45. if(qty <= 2016) {         
  46. y =  qty * <?php echo $row_Recordset9['qty2016']; ?>;          document.prodform.price.value = y;                          }                }                                                                                                                                                                                                };   
  47. }
  48.  
  49.  
Aug 6 '08 #21

acoder
Expert Mod 15k+
P: 16,027
You can't have multiple else statements, but you can have multiple else-if statements. So
Expand|Select|Wrap|Line Numbers
  1. if (cond) {
  2. ...
  3. } else if (cond) {
  4.   ...
  5. } else if (cond) {
  6.   ...
  7. } else {
  8.   ...
  9. }
would be ok, but
Expand|Select|Wrap|Line Numbers
  1. if (cond) {
  2. ...
  3. } else {
  4.   if (cond) {
  5.   ...
  6.   }
  7. } else {
  8.   if (cond) {
  9.   ...
  10.   }
  11. } else {
  12.   ...
  13. }
wouldn't be.
Aug 6 '08 #22

P: 22
this thing still isnt right. instead of cycling thru the if else if statement it either chooses between qty12 or qty2016 and nothing in the middle and if i punch in a different number it still references it's original calculation.... here's the newest, please let me know.

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (qty <= 12 && qty >= 23) {  with this one the min they can order is 12           
  3. y =  qty * 3.17;         
  4. document.prodform.price.value = y;         
  5. alert(y);
  6.  
  7. } else if (qty <= 24 && qty >= 35) {             
  8. y =  qty * 2.95;          
  9. document.prodform.price.value = y;         
  10. alert(y);
  11.  
  12. } else if (qty <= 36 && qty >= 71) {             
  13. y =  qty * 2.72;         
  14. document.prodform.price.value = y;            
  15. alert(y);              
  16.  
  17. } else if (qty <= 72 && qty >= 143) {             
  18. y =  qty * 2.27;          
  19. document.prodform.price.value = y;            
  20. alert(y);        
  21.  
  22. } else if (qty <= 144 && qty >= 287) {            
  23. y =  qty * 1.94;          
  24. document.prodform.price.value = y;         
  25. alert(y);         
  26.  
  27. } else if (qty <= 288 && qty >= 575) {            
  28. y =  qty * 1.92;          
  29. document.prodform.price.value = y;         
  30. alert(y);                  
  31.  
  32. } else if(qty <= 576 && qty >= 1007){              
  33. y =  qty * 1.88;          
  34. document.prodform.price.value = y;             alert(y);                          
  35.  
  36. } else if (qty <= 1008 && qty >= 2015) {              
  37. y =  qty * 1.85;              
  38. document.prodform.price.value = y;             alert(y);                      
  39.  
  40. } else if  (qty <= 2016){              
  41. y =  qty * 1.81;          
  42. document.prodform.price.value = y;         
  43. alert(y);                  
  44.  
  45. }
  46.  
  47.  



You can't have multiple else statements, but you can have multiple else-if statements. So
Expand|Select|Wrap|Line Numbers
  1. if (cond) {
  2. ...
  3. } else if (cond) {
  4.   ...
  5. } else if (cond) {
  6.   ...
  7. } else {
  8.   ...
  9. }
would be ok, but
Expand|Select|Wrap|Line Numbers
  1. if (cond) {
  2. ...
  3. } else {
  4.   if (cond) {
  5.   ...
  6.   }
  7. } else {
  8.   if (cond) {
  9.   ...
  10.   }
  11. } else {
  12.   ...
  13. }
wouldn't be.
Aug 7 '08 #23

acoder
Expert Mod 15k+
P: 16,027
I think the logic has gone a bit awry. You need to swap the <= and >= around, so
Expand|Select|Wrap|Line Numbers
  1. if (qty <= 12 && qty >= 23) { 
becomes
Expand|Select|Wrap|Line Numbers
  1. if (qty >= 12 && qty <= 23) { 
Aug 7 '08 #24

P: 22
but that doesnt explain why it only uses the qty2016 value.

the first one has to be if qty greater than or equal to12 and is also less than 23, calculate the quantity by 3.17 (php echo recordset1)

if quantity is greater than or equal to 24 and is also less than or equal to 35, calculate quantity by 2.95 (php echo recordset2)

and so on til it gets to 2016. also something else that has me confused is why it doesnt redo it if the quantity number is changed at anytime.


I think the logic has gone a bit awry. You need to swap the <= and >= around, so
Expand|Select|Wrap|Line Numbers
  1. if (qty <= 12 && qty >= 23) { 
becomes
Expand|Select|Wrap|Line Numbers
  1. if (qty >= 12 && qty <= 23) { 
Aug 7 '08 #25

acoder
Expert Mod 15k+
P: 16,027
The reason why it only gives the qty2016 value is because that's the only condition that can be met. Think about it. If you have qty <=12 and qty >=23, then the qty value has to be less than or equal to 12 AND greater than or equal to 23 which is impossible!
Aug 7 '08 #26

P: 22
ok acoder, how about this?

could i do qty <= 12 && qty > 24?

that way the logic would be asking if this quantity is in between these two numbers? would that be possible???
Aug 7 '08 #27

acoder
Expert Mod 15k+
P: 16,027
No, see post #24. It should be qty >= 12 && qty < 24
Aug 7 '08 #28

P: 22
IT WORKS! acoder, thanks for all your help you have no idea how much i really appreciate it.

thanks to bytes for having ppl on here like you that we can talk to inorder to get help.

this will be the last question for this thread. is there any way i can "fix" the decimals to two places? sometimes i ll get a value that looks like this: 1586.7199999999998 instead of making it 1586.72.



No, see post #24. It should be qty >= 12 && qty < 24
Aug 7 '08 #29

acoder
Expert Mod 15k+
P: 16,027
Glad to hear that it's working.

As for the last question, use the toFixed() method.
Aug 7 '08 #30

P: 22
worked perfectly thank you again acoder
Aug 8 '08 #31

acoder
Expert Mod 15k+
P: 16,027
You're welcome :)
Aug 8 '08 #32

P: 22
mod pls close thread
Aug 14 '08 #33

Post your reply

Sign in to post your reply or Sign up for a free account.