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

3
  • 1
    U must use ajax for this, use jquery api.jquery.com/jQuery.ajax Commented Sep 3, 2013 at 12:01
  • 1
    With an Ajax request. Commented Sep 3, 2013 at 12:01
  • 3
    There are many duplicates - please search before posting Commented Sep 3, 2013 at 12:02

6 Answers 6

18

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it is same like Java JSP + JavaScript. All programming languages are almost the same in basic logics. Thank you dragoste and all others
15

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.

1 Comment

This is a much more constructive answer that actually answers the question.
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 );                    
        }
        );

1 Comment

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

Comments

0

use jquery

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

            // put your response where you want to  

            }
        }); 

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.