Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <a onclick="call_php('php_function_name', ['arg_1', 'arg_2']);">call</a>
- <p id="demo"></p>
- function call_php(fname, args) {
- var xhttp = new XMLHttpRequest();
- xhttp.onreadystatechange = function () {
- if (this.readyState == 4 && this.status == 200) {
- document.getElementById('demo').innerHTML = this.responseText;
- }
- };
- xhttp.open('POST', 'includes/functions.php', true);
- xhttp.setRequestHeader('Content-type', 'application/json');
- xhttp.send(JSON.stringify({
- 'fname': fname,
- 'args': args
- }));
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- //header('Content-Type: application/json');
- $post = json_decode($_POST/*, true*/);
- // DO SOMETHING TO CALL: fname(...args);
- }
- function php_function_name(arg_1, arg_2) {
- // A FUNCTION...
- }
- $postdata = file_get_contents("php://input");
- <?php
- switch ($_SERVER['REQUEST_METHOD']) {
- case 'POST':
- post_handler();
- return;
- case 'GET':
- get_handler();
- return;
- default:
- echo 'Not implemented';
- return;
- }
- function post_handler() {
- $raw_data = file_get_contents("php://input");
- $req_body = json_decode($raw_data, TRUE);
- call_user_func_array($req_body['function'], $req_body['args']);
- }
- function get_handler() {
- echo 'This page left intentionally blank.';
- }
- function some_function($arg1, $arg2) {
- echo $arg1.' '.$arg2;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement