www.webdeveloper.com
Results 1 to 5 of 5

Thread: In a php file i can acces from javascript to the php variables but not functions

  1. #1
    Join Date
    Jan 2013
    Posts
    84

    Unhappy In a php file i can acces from javascript to the php variables but not functions

    I've an html file with php extension (index.php) and i use a mix of javascript and php code.
    The problem is that i'm unable to launch a php function from JS..

    this don't work
    PHP Code:
    <?php 
    function updateVideo(){
    //some code
    }?>

    <script>
    function aggiornaVideo(){
        <?php updateVideo(); ?>
    }

    </script>
    Yes, i know that PHP is a server side language while JS a client-side, but for what reason if i attempt to access to php variables along the document it's possibile such they are stored in memory while the same thing isn't possible with function?
    For exemple, this code normally retrieve me a variable from php

    PHP Code:
    <?php 
     $page
    =4
    //some code
    ?>

    <script>
    function aggiornaVideo()
    {
        var pro='<?php echo $page?>'
        alert(pro)
    }

    </script>
    Last edited by American horizo; 07-29-2013 at 10:52 AM.

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    What you are doing with the variables is basically converting them to Javascript. When you echo out the variable, it basically writes code that now says:
    HTML Code:
    <script>
    function aggiornaVideo()
    {
        var pro='4'
        alert(pro)
    }
    
    </script>
    What you should do is make an AJAX call to a page with the PHP function you want to execute.

    Here is a simple tutorial: http://www.tizag.com/ajaxTutorial/index.php

  3. #3
    Join Date
    Jan 2013
    Posts
    84
    Yes i can use the ajax call, but technically i don't uderstand how is possibile to retrieve php variable such they are store in client memory, while function don't

    What you are doing with the variables is basically converting them to Javascript.
    yes but this assumes that once the page is executed, the php variable is keeped in the client and can be retrieved. Why the rest of code not?

  4. #4
    Join Date
    Dec 2012
    Posts
    101
    PHP is executed before the page is loaded, JavaScript runs after the page has loaded. The PHP variable isn't kept on the client, what you've done there is use PHP to create a JavaScript variable.

    It's impossible to call anything set in PHP using JavaScript without sending a request to another page using something like AJAX.

  5. #5
    Join Date
    Jan 2013
    Posts
    84

    Question

    Quote Originally Posted by iBeZi View Post
    PHP is executed before the page is loaded, JavaScript runs after the page has loaded. The PHP variable isn't kept on the client, what you've done there is use PHP to create a JavaScript variable.

    It's impossible to call anything set in PHP using JavaScript without sending a request to another page using something like AJAX.
    so, doing
    var someVar ='<?php echo $phpVar;?>'

    is like a sort of call to the server-side script?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles