8

I am a newbie to php

   <?php
    getDBData(){
        //log the call
        $fetchedData = myDbCode.fetchData();
        return 
    }
  ?>
  <script type="text/javascript">
      dbData = <?php echo json_encode(getDBData()); ?>
  </script>

As observed in the log that getDBData get called only once during the page loading and later on even with dbData = <?php echo json_encode(getDBData()); ?> this code the call to getDBData() doesn't happen.

Any idea why the call to getDBData() happening only on page load and not thenafter

How to call getDBData() from javascript

veer7
  • 20,074
  • 9
  • 46
  • 74

6 Answers6

17

You don't actually understand, how it works.

Javascript is a client-side language, which means, that it executes in web browser. PHP is server-side which mean it executes on server.

While handling request, first PHP is executed, that the response is returned to user, and then Javacript executes.

To communicate between client and server you can use ajax requests, which are basically simple http requests but without reloading whole page.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
  • Yes, it is same like Java JSP + JavaScript. All programming languages are almost the same in basic logics. Thank you dragoste and all others – veer7 Sep 04 '13 at 06:38
14

You should use Ajax for that. I.e. you have a php file which returns the output of the function:

// data.php
<?php
    function getDBData(){
        //log the call
        $fetchedData = myDbCode.fetchData();
        return $fetchedData;
    }
    echo getDBData();
?>

// html file
<script type="text/javascript">
    var getDBData = function(callback) {
        $.ajax({
            url: "data.php"
        }).done(callback);
    }
    var dbData = <?php echo json_encode(getDBData()); ?>
    getDBData(function(data) {
        dbData = data;
    })
</script>

The code above uses jQuery.

Krasimir
  • 13,306
  • 3
  • 40
  • 55
1

you can used AJAX for get server side php vaue into javascript variable read this ajax example and implement it.

            // Launch AJAX request.
            $.ajax(
                {
                    // The link we are accessing.
                    url: jLink.attr( "href" ),

                    // The type of request.
                    type: "get",

                    // The type of data that is getting returned.
                    dataType: "html",

                    error: function(){
                        ShowStatus( "AJAX - error()" );

                        // Load the content in to the page.
                        jContent.html( "<p>Page Not Found!!</p>" );
                    },

                    beforeSend: function(){
                        ShowStatus( "AJAX - beforeSend()" );
                    },

                    complete: function(){
                        ShowStatus( "AJAX - complete()" );
                    },

                    success: function( strData ){
                        ShowStatus( "AJAX - success()" );

                        // Load the content in to the page.
                        jContent.html( strData );
                    }
                }                           
                );

            // Prevent default click.
            return( false );                    
        }
        );
Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
  • 1
    He doesn't even understand the idea of client and server side, and you give him 20~ line jQuery code with ajax request? This is not the way. – Jakub Matczak Sep 03 '13 at 12:07
0

You can do it through ajax.

Here is a link here to do it with jquery : using jquery $.ajax to call a PHP function

Community
  • 1
  • 1
kmas
  • 6,401
  • 13
  • 40
  • 62
0

use jquery

$.ajax({
            url: 'yourpage.php',
            type: 'POST',
            data:'',
            success: function(resp) {

            // put your response where you want to  

            }
        }); 
Voonic
  • 4,667
  • 3
  • 27
  • 58
0

You can't directly call PHP functions from javascript.

You have to "outsource" the getDBDate to an own .php file where you output the json_encoded string and call this file with ajax and get the output of the page.

The easiest to do AJAX requests in javascript is to use the JQuery Library: http://api.jquery.com/jQuery.ajax/

virtualmarc
  • 533
  • 1
  • 4
  • 16