Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. <a onclick="call_php('php_function_name', ['arg_1', 'arg_2']);">call</a>
  2. <p id="demo"></p>
  3.  
  4. function call_php(fname, args) {
  5. var xhttp = new XMLHttpRequest();
  6. xhttp.onreadystatechange = function () {
  7. if (this.readyState == 4 && this.status == 200) {
  8. document.getElementById('demo').innerHTML = this.responseText;
  9. }
  10. };
  11. xhttp.open('POST', 'includes/functions.php', true);
  12. xhttp.setRequestHeader('Content-type', 'application/json');
  13. xhttp.send(JSON.stringify({
  14. 'fname': fname,
  15. 'args': args
  16. }));
  17. }
  18.  
  19. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  20. //header('Content-Type: application/json');
  21. $post = json_decode($_POST/*, true*/);
  22. // DO SOMETHING TO CALL: fname(...args);
  23. }
  24. function php_function_name(arg_1, arg_2) {
  25. // A FUNCTION...
  26. }
  27.  
  28. $postdata = file_get_contents("php://input");
  29.  
  30. <?php
  31.  
  32. switch ($_SERVER['REQUEST_METHOD']) {
  33. case 'POST':
  34. post_handler();
  35. return;
  36. case 'GET':
  37. get_handler();
  38. return;
  39. default:
  40. echo 'Not implemented';
  41. return;
  42. }
  43.  
  44. function post_handler() {
  45. $raw_data = file_get_contents("php://input");
  46. $req_body = json_decode($raw_data, TRUE);
  47. call_user_func_array($req_body['function'], $req_body['args']);
  48. }
  49.  
  50. function get_handler() {
  51. echo 'This page left intentionally blank.';
  52. }
  53.  
  54. function some_function($arg1, $arg2) {
  55. echo $arg1.' '.$arg2;
  56. }
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement