How can I call PHP functions by JavaScript?

后端 未结 11 1189
情话喂你
情话喂你 2020-11-22 12:08

I am trying to call a PHP function from an external PHP file into a JavaScript script. My code is different and large, so I am writing a sample code here.

This is m

相关标签:
11条回答
  • 2020-11-22 12:46

    I created this library, may be of help to you. MyPHP client and server side library

    Example:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
    
        <!-- include MyPHP.js -->
        <script src="MyPHP.js"></script>
    
        <!-- use MyPHP class -->
        <script>
            const php = new MyPHP;
            php.auth = 'hashed-key';
    
            // call a php class
            const phpClass = php.fromClass('Authentication' or 'Moorexa\\Authentication', <pass aguments for constructor here>);
    
            // call a method in that class
            phpClass.method('login', <arguments>);
    
            // you can keep chaining here...
    
            // finally let's call this class
            php.call(phpClass).then((response)=>{
                // returns a promise.
            });
    
            // calling a function is quite simple also
            php.call('say_hello', <arguments>).then((response)=>{
                // returns a promise
            });
    
            // if your response has a script tag and you need to update your dom call just call
            php.html(response);
    
        </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 12:49

    use document.write for example,

    <script>
      document.write(' <?php add(1,2); ?> ');
      document.write(' <?php milt(1,2); ?> ');
      document.write(' <?php divide(1,2); ?> ');
    </script>
    
    0 讨论(0)
  • 2020-11-22 12:50

    If you actually want to send data to a php script for example you can do this:

    The php:

    <?php
    $a = $_REQUEST['a'];
    $b = $_REQUEST['b']; //totally sanitized
    
    echo $a + $b;
    ?>
    

    Js (using jquery):

    $.post("/path/to/above.php", {a: something, b: something}, function(data){                                          
      $('#somediv').html(data);
    });
    
    0 讨论(0)
  • 2020-11-22 12:54

    You need to create an API : Your js functions execute AJAX requests on your web service

      var mult = function(arg1, arg2)
        $.ajax({
          url: "webservice.php?action=mult&arg1="+arg1+"&arg2="+arg2
        }).done(function(data) {
          console.log(data);
        });
      }
    

    on the php side, you'll have to check the action parameter in order to execute the propre function (basically a switch statement on the $_GET["action"] variable)

    0 讨论(0)
  • 2020-11-22 12:55

    Try This

    <script>
      var phpadd= <?php echo add(1,2);?> //call the php add function
      var phpmult= <?php echo mult(1,2);?> //call the php mult function
      var phpdivide= <?php echo divide(1,2);?> //call the php divide function
    </script>
    
    0 讨论(0)
提交回复
热议问题