0

I would to know if its possible to call a PHP function from a external file using XMLHttpRequest (AJAX) like in this form next.

index.php

<script>

            var xhr = new XMLHttpRequest();
            xhr.open('POST','functions.php',true);
            
            xhr.onload = function(){
            //Here it is where I would like to call the function 
            console.log(this.responseText);
            }
            xhr.send(params);  
        }
</script>

functions.php

<?php

    function hello(){
      echo "Hello from function php";
    }

?>

In my project the code I am using is more complex but the example above is simpler and it is the way I would like to call my functions using PHP and AJAX, I was thinking adding <?php hello(); ?> inside xhr.onload function but It didn't work, is there any better idea of how to do this ? I will apprecciete any help..

cesar561
  • 13
  • 6
  • 1
    Read the above link and remember that JavaScript (your ajax code) is client side while PHP is server side. Also, I think you've misunderstood what Ajax is. An Ajax request is just a request to the server, like when you go to a site using the browser. It doesn't return the PHP code/functions. It returns the output (like HTML or any echo's). – M. Eriksson Jul 24 '20 at 20:54
  • @Magnus Eriksson thank you, so you mean it's never possible to call a specific and unique function from a php file with several functions inside? All I can do is to call the full PHP script?? – cesar561 Jul 24 '20 at 21:20
  • All you can do is call a URL. Then it's up to the server to handle the request. If it is to a PHP file, then you need to write all PHP code there and just return the result. Browsers can't read or execute PHP code. – M. Eriksson Jul 24 '20 at 21:31
  • @Magnus Eriksonn Hello again, sorry but I still confused with what you say, so you mean with call a URL in my example and using AJAX would be functions.php because it is located in the server side so this file makes all process of data and AJAX in my example will only work to retrieve this data that has already processed by this PHP file? Sorry I am a little confused with all this . – cesar561 Jul 25 '20 at 14:26
  • @Magnus Erinsonn so we can say it's not a good practice to call functions in PHP this way – cesar561 Jul 25 '20 at 14:29
  • That explanation was pretty spot on so you're probably less confused than you think :-) You can do as the example in the answer below, pass the action you want to perform in the request as an argument and then call the correct function in PHP based on that argument. Not the one using `eval()` though. That should never ever be used for something like this. – M. Eriksson Jul 25 '20 at 15:27

1 Answers1

4

What you need to do is make the PHP function run when the PHP file is triggered. To do this, add hello(); at the bottom of functions.php. To have multiple functions, you could have a folder of php files that all do one single function.

If you wanted to have a PHP file with multiple functions that the JS chooses, you could do something like this:

Functions.php

<?php

$function = $_POST["function"];

if ($function == "hello") {
    hello();
}
else if ($function == "goodbye") {
    goodbye();
}

function hello() {
    echo "Hello from function php";
}

function goodbye() {
    echo "Goodbye from function php";
}

?>

index.php


<script>

var xhr = new XMLHttpRequest();
xhr.open('POST','functions.php',true);
xhr.onload = function() {
    console.log(this.responseText);
    // If you wanted to call the function in here, then just make another whole xhr var and send it in this function
}

xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('function=hello');
</script>

Eval could work, but it is an extremely bad idea to use it on data sent by the client (as in a malicious request could delete and read all sorts of data on the server)

ThatCoolCoder
  • 249
  • 3
  • 16
  • 1
    No please! Don't even _mention_ `eval` in this context! Using that to run what the client sends in would _way beyond_ crazy! That's like open the doors wide open to their server! – M. Eriksson Jul 24 '20 at 21:04