473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 10297
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

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

Similar topics

1
by: vishnu mahendra | last post by:
i am new to javascript. Can any one please tell me how to connect javascript to oracle in windows 98 using apache.where i could get information on connecting? thank you in advance, vishnu
3
by: Scott Castillo | last post by:
Is it possible to access an SQL Server database from a Javascript client? I know you can transfer variables from middleware to Javascript client, but was wondering if you can directly access...
3
by: Nuno Paquete | last post by:
Hi group. I need to save HTML to a database, but I have to be sure that these text is well saved, I mean, imagine that you've got a <TEXTAREA> and want to save the text inside. If the user type an...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
5
by: aaronwmail-usenet | last post by:
Hi folks. I'd really like some comments or flames please :). I've ported my xsdb database engine to javascript. I put up a demo page here: ...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
5
by: Navodit | last post by:
Hi I want to write code in Javascript which pulls out entries from a database via a for loop. Essentially what I am trying to do is something like: //setting up database connection <% Set...
2
by: Methis Elddir | last post by:
Can javascript be used to build a web-based front end to a local database? I wrote an application for a client that controls a series of medical devices. It runs a series of tests, collects...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.