469,935 Members | 1,539 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

How to retrieve data from database to use in a javascript app

Hi. I have a calculate.js javascript file where all the function that execute the calculation is consist in this file. Now, I want to retrieve a data from the database for calculation and be done in this calculate.js file. I have search a lot of methods to do this but most said cannot do this from a client-side. Is it possible? How to achieve this? Thanks in advance.
Oct 11 '10 #1
9 9764
gits
5,390 Expert Mod 4TB
what database do you have? should it be cross-browser compatible? if it's not only for IE you would need to involve some serverside code ... otherwise you might have luck with some ActiveX controls ...
Oct 11 '10 #2
gits, thanks for the reply. Ya, it is cross-browser compatible. My database is MySQL. What should I do? Thanks.
Oct 11 '10 #3
gits
5,390 Expert Mod 4TB
you might use an XMLHttpRequest to retrieve the data - for a start you might go through this tutorial which will show the basic usage with ASP or PHP.
Oct 11 '10 #4
I'm still not very clear. Can I write my ajax and javascript code in calculate.js file? So that I can do my calculation in calculate.js file. Moreover, how can I just retrieve a specific data from database to use in the calculation? Thanks in advance.
Oct 12 '10 #5
gits
5,390 Expert Mod 4TB
you only have to ensure that the response is ready when you want to calculate something ... the javascript code that triggers the request could be written wherever you want to have it - you just have to ensure the correct 'flow'.

to get the value from the database you even have to write a serverside script that requests the database ... this would probably execute a SQL-statement ... so of course you could retrieve a specific value ...
Oct 12 '10 #6
Thanks for the reply. But can you show me the way to do calculation? Because I have to retrieve data from database for calculation. For example, the data that I retrieved from database is $a (php), then I have to use this value in javascipt calculation.

for example, this is the code that I have to write in calculate.js file.

Expand|Select|Wrap|Line Numbers
  1. var payment = document.getElementById("paymentA").value = (document.getElementById("paymentB").value * ($a/100));
  2.  
although I know this can't work but this is something that I have to do with. So do you have any ideas or example on this? Thanks in advance.
Oct 14 '10 #7
gits
5,390 Expert Mod 4TB
if you retrieve it via an ajax-call then the value is in you response-text - there are many tutorials out there - for example the one i already mentioned.

so what do you have so far to retrieve the value?
Oct 15 '10 #8
still not very clear because ajax is totally a new thing for me. What do you mean by what I have so far to retrieve the value? Is it the way I retrieve my value? Somethings as below,
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT percent FROM stock WHERE id = '$id'";
  2. $result = mysql_query($sql);
  3. $row = mysql_fetch_array($result);
  4.  
So I will use the percent in my calculation. Thanks in advance for the help.
Oct 15 '10 #9
Thanks for the help and now I manage to retrieve the data that I am needed from database. Then the data will be used in calculation. But now the system will read the correct value and path after the calculate button is clicked but not work with the others code.
Part of the codes.

Javascript code
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function a()
  3. {
  4.     var a= "<?php echo $ax; ?>";
  5.     var b= "<?php echo $bx; ?>";
  6.     var c= "<?php echo $cx; ?>";
  7.     var d= "<?php echo $dx; ?>";
  8.  
  9.     if(c== "xxx")
  10.     {
  11.         abc();
  12.     }
  13.  
  14.     else if(c== "yyy")
  15.     {
  16.         def(); 
  17.     }                    
  18. }
  19. function abc()
  20. {
  21.     if(a == "")
  22.     {
  23.         if(b == "Yes")
  24.         {
  25.            //some coding here
  26.         }
  27.  
  28.         else if(b == "")
  29.         {
  30.             //some coding here
  31.         }
  32.     }
  33.  
  34.     else if(a == "Yes")
  35.     { 
  36.         if(b == "Yes")
  37.         {//alert("hi"); For example, I try to pop up a msg here, then it got run but the rest of the coding not working..every line same.the got read the correct condition but didn't execute
  38.             var ab = document.getElementById("x").value;
  39.             var cd = document.getElementById("y").value = (d * (ab/100));
  40.             document.getElementById("y").value = cd.toFixed(2);
  41.  
  42.             //some coding here            
  43.             if(document.main.zzz.options[document.main.zzz.selectedIndex].value == "xy")
  44.             {
  45.                 var ef = document.getElementById("t").value = ((document.getElementById("s").value - document.getElementById("o").value)*(50/100));
  46.                 //some coding here
  47.             }
  48.  
  49.             else if(document.main.zzz.options[document.main.zzz.selectedIndex].value == "st")
  50.             {
  51.                 //some coding here
  52.             }
  53.         }
  54.  
  55.         else if(b == "")
  56.         {            
  57.             //some coding here
  58.         }
  59.     }
  60. }
  61. function def()
  62. {
  63.    //some coding here
  64. }
  65.  
php code
Expand|Select|Wrap|Line Numbers
  1. <table border="1" align="center">
  2. <tr>
  3. <td align="center"><input type="button" name="calculate" value="Calculate" onClick="a()"></td>
  4. <td align="center"><input type="submit" name="submit" value="Submit"></td>
  5. <td align="center"><input type="reset" name="reset" value="Reset"></td>
  6. </tr>
  7. </table>
  8.  
Thanks in advance for any advice and solution.
Oct 21 '10 #10

Post your reply

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

Similar topics

1 post views Thread by vishnu mahendra | last post: by
3 posts views Thread by Scott Castillo | last post: by
3 posts views Thread by Nuno Paquete | last post: by
136 posts views Thread by Matt Kruse | last post: by
5 posts views Thread by aaronwmail-usenet | last post: by
5 posts views Thread by Navodit | last post: by
2 posts views Thread by Methis Elddir | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.