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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StarLavender
    New Member
    • Apr 2010
    • 33

    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.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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 ...

    Comment

    • StarLavender
      New Member
      • Apr 2010
      • 33

      #3
      gits, thanks for the reply. Ya, it is cross-browser compatible. My database is MySQL. What should I do? Thanks.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        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.

        Comment

        • StarLavender
          New Member
          • Apr 2010
          • 33

          #5
          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

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            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

            • StarLavender
              New Member
              • Apr 2010
              • 33

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

              Code:
              var payment = document.getElementById("paymentA").value = (document.getElementById("paymentB").value * ($a/100));
              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.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                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?

                Comment

                • StarLavender
                  New Member
                  • Apr 2010
                  • 33

                  #9
                  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,
                  Code:
                  $sql = "SELECT percent FROM stock WHERE id = '$id'";
                  $result = mysql_query($sql);
                  $row = mysql_fetch_array($result);
                  So I will use the percent in my calculation. Thanks in advance for the help.

                  Comment

                  • StarLavender
                    New Member
                    • Apr 2010
                    • 33

                    #10
                    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
                    Code:
                    <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
                    }
                    php code
                    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>
                    Thanks in advance for any advice and solution.

                    Comment

                    Working...