-2

I need to call a couple small PHP functions that should execute after the user makes changes in the DOM. I know this can be done by capturing the change in jquery, then pointing an ajax call to a separate script with the function inside it; but having a few of these small functions I would rather not make a file for each, if possible. As such, is there a way to include the PHP code directly within the ajax call and not go to a separate url?

EDIT 1

Assuming a post request is being made, something like the following is what I'd like to do:

$("select").change(function() {
    $.post("<?php ... ?>", {option: $(this).val()}, function(return) {
        //do stuff with results
    }
});
yanman1234
  • 1,009
  • 9
  • 27
  • To address the duplicate marking, I understand that this cannot be done client side, my question is if I can execute php code through an ajax call (or any other means) without having to make a specific script for each type of call. The php would obviously have to be executed server side and I am unaware if there is another mechanism to do so besides premade scripts. – yanman1234 Jun 27 '17 at 13:30

1 Answers1

0

With ajax you can call a php file on the server but not on client side. If you want to run php code directly on client side, you'd need to install php on each client which, I really don't recommend.

Alex Dupuis
  • 366
  • 3
  • 14
  • 1
    "install php on each client"? I think this answer is as bad as the question... – CarlosAS Jun 27 '17 at 13:26
  • I totally agree, that's why "I really don't recommend" that but, it's the only way he could do that, I don't know his requirement, he may have some reason what wanting to do that. – Alex Dupuis Jun 27 '17 at 13:30
  • I have edited the question to try and be more clear. In a broad sense, I'd like to execute php without having to make a bunch of scripts to make requests to. – yanman1234 Jun 27 '17 at 13:32
  • 1
    The best way would be to make 1 script with parameters for the variations tou could have. I can't see what code you'd like to do in php on client side that you could'nt with javascript, which is the right script language on client side. – Alex Dupuis Jun 27 '17 at 13:37