0

When I have to get something from my server and pass some value to it I'm currently using $.post('myserver/formhandler.php', { 'v' : value }), and in my formhandler.php file I have the following:

require_once("mainclass.php");
require_once("config.php"); 

if(isset($_POST['v'])) {
    $mainclass->FunctionA($_POST['v']);
}
if(isset($_POST['s'])) {
    $mainclass->FunctionB($_POST['s']);
}

and so on. The thing is that sometimes I want to load something without passing any value, and my doubt is if it's faster do use the same $.post() function and pass a variable just to be able to trigger some function in the same file or if it would be better to use $.load() and have one specific file for each function, like $.load('LoadFunctionC.php'), where LoadFunctionC.php would be something like:

require_once("mainclass.php");
require_once("config.php"); 

$mainclass->FunctionC();

Would it slow down my website for, for example, calling mainclass.php and config.php multiples times? Thanks!

sorold
  • 475
  • 7
  • 17
  • Please check out http://ericlippert.com/2012/12/17/performance-rant/ and see if you can make your post more specific/answerable on SO. – Alexei Levenkov Feb 13 '16 at 04:25

1 Answers1

1

If you look at the $.load docs, you will see that $.load, just like $.post and $.get (and a few others) are really just calling $.ajax. Thus, there shouldn't be any significant performance change among them.

A better way to do this might be to give each endpoint its own file. That way you just choose in the JS which endpoint to hit. Why make your server do work (and make your scripts more complicated) when you can get the user's browser to do the work for you!

var url = '';
switch (someCase) {
  case 'v':
    url = 'doThingV.php';
    break;
  case 's':
    url = 'doThingS.php';
    break;
  default:
    url = 'doThingC.php';
}

$.ajax({
  url: url,
  ...
});
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • Thanks!That's what I was looking for! I'd just like to understand one last thing: since every file has require_once, doesn't it slow down my server since it has to load the file for each call? – sorold Feb 13 '16 at 12:43
  • I think [this is a good thread](http://stackoverflow.com/questions/186338/why-is-require-once-so-bad-to-use) for you to read though regarding that. It's a little dated, but you'll get the general idea. – Matthew Herbst Feb 13 '16 at 16:49
  • Thanks for giving me directions. That's it. – sorold Feb 13 '16 at 19:58