How to call a JavaScript function from PHP?

后端 未结 10 2357
误落风尘
误落风尘 2020-11-21 23:03

How to call a JavaScript function from PHP?

The followi

相关标签:
10条回答
  • 2020-11-21 23:23

    I always just use echo "<script> function(); </script>"; or something similar. you're not technically calling the function in PHP, but this as close as your going to get.

    0 讨论(0)
  • 2020-11-21 23:31

    You can't. You can call a JS function from HTML outputted by PHP, but that's a whole 'nother thing.

    0 讨论(0)
  • 2020-11-21 23:32

    Thats not possible. PHP is a Server side language and JavaScript client side and they don't really know a lot about each other. You would need a Server sided JavaScript Interpreter (like Aptanas Jaxer). Maybe what you actually want to do is to use an Ajax like Architecture (JavaScript function calls PHP script asynchronously and does something with the result).

    <td onClick= loadxml()><i>Click for Details</i></td>
    
    function loadxml()
    {
        result = loadScriptWithAjax("/script.php?event=button_clicked");
        alert(result);
    }
    
    // script.php
    <?php
        if($_GET['event'] == 'button_clicked')
            echo "\"You clicked a button\"";
    ?>
    
    0 讨论(0)
  • 2020-11-21 23:32

    You may not be able to directly do this, but the Xajax library is pretty close to what you want. I will demonstrate with an example. Here's a button on a webpage:

    <button onclick="xajax_addCity();">Add New City</button> 
    

    Our intuitive guess would be that xajax_addCity() is a Javascript function, right? Well, right and wrong. The cool thing Xajax allows is that we don't have any JS function called xajax_addCity(), but what we do have is a PHP function called addCity() that can do whatever PHP does!

    <?php function addCity() { echo "Wow!"; } ?>
    

    Think about it for a minute. We are virtually invoking a PHP function from Javascript code! That over-simplified example was just to whet the appetite, a better explanation is on the Xajax site, have fun!

    0 讨论(0)
  • As far as PHP is concerned (or really, a web server in general), an HTML page is nothing more complicated than a big string.

    All the fancy work you can do with language like PHP - reading from databases and web services and all that - the ultimate end goal is the exact same basic principle: generate a string of HTML*.

    Your big HTML string doesn't become anything more special than that until it's loaded by a web browser. Once a browser loads the page, then all the other magic happens - layout, box model stuff, DOM generation, and many other things, including JavaScript execution.

    So, you don't "call JavaScript from PHP", you "include a JavaScript function call in your output".

    There are many ways to do this, but here are a couple.

    Using just PHP:

    echo '<script type="text/javascript">',
         'jsfunction();',
         '</script>'
    ;
    

    Escaping from php mode to direct output mode:

    <?php
        // some php stuff
    ?>
    <script type="text/javascript">
        jsFunction();
    </script>
    

    You don't need to return a function name or anything like that. First of all, stop writing AJAX requests by hand. You're only making it hard on yourself. Get jQuery or one of the other excellent frameworks out there.

    Secondly, understand that you already are going to be executing javascript code once the response is received from the AJAX call.

    Here's an example of what I think you're doing with jQuery's AJAX

    $.get(
        'wait.php',
        {},
        function(returnedData) {
            document.getElementById("txt").innerHTML = returnedData;
    
            //  Ok, here's where you can call another function
            someOtherFunctionYouWantToCall();
    
            // But unless you really need to, you don't have to
            // We're already in the middle of a function execution
            // right here, so you might as well put your code here
        },
        'text'
    );
    
    function someOtherFunctionYouWantToCall() {
        // stuff
    }
    

    Now, if you're dead-set on sending a function name from PHP back to the AJAX call, you can do that too.

    $.get(
        'wait.php',
        {},
        function(returnedData) {
            // Assumes returnedData has a javascript function name
            window[returnedData]();
        },
        'text'
    );
    

    * Or JSON or XML etc.

    0 讨论(0)
  • 2020-11-21 23:39

    PHP runs in the server. JavaScript runs in the client. So php can't call a JavaScript function.

    0 讨论(0)
提交回复
热议问题