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.
How to retrieve data from database to use in a javascript app
Collapse
X
-
Tags: None
-
gits, thanks for the reply. Ya, it is cross-browser compatible. My database is MySQL. What should I do? Thanks.Comment
-
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.Comment
-
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 ...Comment
-
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.
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.Code:var payment = document.getElementById("paymentA").value = (document.getElementById("paymentB").value * ($a/100));Comment
-
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,
So I will use the percent in my calculation. Thanks in advance for the help.Code:$sql = "SELECT percent FROM stock WHERE id = '$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result);
Comment
-
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
php codeCode:<script type="text/javascript"> function a() { var a= "<?php echo $ax; ?>"; var b= "<?php echo $bx; ?>"; var c= "<?php echo $cx; ?>"; var d= "<?php echo $dx; ?>"; if(c== "xxx") { abc(); } else if(c== "yyy") { def(); } } function abc() { if(a == "") { if(b == "Yes") { //some coding here } else if(b == "") { //some coding here } } else if(a == "Yes") { if(b == "Yes") {//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 var ab = document.getElementById("x").value; var cd = document.getElementById("y").value = (d * (ab/100)); document.getElementById("y").value = cd.toFixed(2); //some coding here if(document.main.zzz.options[document.main.zzz.selectedIndex].value == "xy") { var ef = document.getElementById("t").value = ((document.getElementById("s").value - document.getElementById("o").value)*(50/100)); //some coding here } else if(document.main.zzz.options[document.main.zzz.selectedIndex].value == "st") { //some coding here } } else if(b == "") { //some coding here } } } function def() { //some coding here }
Thanks in advance for any advice and solution.Code:<table border="1" align="center"> <tr> <td align="center"><input type="button" name="calculate" value="Calculate" onClick="a()"></td> <td align="center"><input type="submit" name="submit" value="Submit"></td> <td align="center"><input type="reset" name="reset" value="Reset"></td> </tr> </table>
Comment
Comment