0

I'm using a lot of AJAX calls where I'll say,

data: {action: "get_categories", etc..}

and then in my PHP I'll say,

if(isset($_POST['action']) && ($_POST['action'] == 'get_categories')) { 
//do stuff
}

I'm trying to refactor the code a bit, and I'd like to take the output of one of these code blocks and pass it into another one, but its not a PHP function, so I'm not really sure how to do that? Is there a way to call a PHP function directly from the AJAX call, rather than using action?

Also, in using this style of PHP code block I have to call the code below in every single code block, and I'm not sure why? I thought if I put it at the top of my php file, and used require_once that might work but nothing I try works unless I include these in every single code block.

require("config.php");
require("database.php");
hyphen
  • 957
  • 1
  • 11
  • 31
  • I don't see any PHP functions in your code above? All looks good to me, not really sure what you are asking? – superphonic Dec 10 '13 at 12:18
  • you can exec function "directly" through AJAX call. like that : `$_POST['action']();` – onionpsy Dec 10 '13 at 12:20
  • @psal Oh god, really?! `POST execute.php HTTP/1.1\n\naction=deleteAllData` – deceze Dec 10 '13 at 12:22
  • @deceze, what ? how do you know he has this kind of function (`deleteAllData`) ? – onionpsy Dec 10 '13 at 12:25
  • @psal I don't, but if I was attempting to hack your site I'd simply try. The point is you're allowing arbitrary code to be executed by anyone. I'm sure you can find some function that will wreak at least minor havoc this way. – deceze Dec 10 '13 at 12:26

1 Answers1

1

No, you cannot call PHP functions from client-side Javascript. First of all, make sure you understand this: What is the difference between client-side and server-side programming?. Having done this, you'll see the only way to execute anything server-side is through an AJAX HTTP call, which is a regular request for a website like any other PHP script would use. HTTP has no concept of "functions" or any particular programming language for that matter. You can request URLs with HTTP, nothing more, nothing less. When these URLs are requested, you can execute PHP code to have these requests handled. Each new request is entirely independent of all other requests, so each one is a standalone script execution which requires its own includes and requires.

Open the network inspector tab in your browser's development tools (Chrome/Safari Web Inspector, Firebug or similar) and watch your AJAX requests go back and forth to the server. This should illustrate the concept a lot better than any explanation would.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • ok, that makes sense, and that's what I was seeing from google searches, but I just felt like I must be missing something. As long as that's the way its supposed to be, then I'm good with it. One additional question...How would I output a variable from one of these PHP code blocks and use it in another one? – hyphen Dec 10 '13 at 12:24
  • I don't understand that last question. – deceze Dec 10 '13 at 12:25
  • 1
    You know what, I just realized my follow-up question is nonsense. I'm trying to think of these code blocks as functions, and they're just not, and I just need to get over it. Thanks for your help! – hyphen Dec 10 '13 at 12:30