-1

when the page loads, it fires a PHP function, from the functions.php file, to grab data from the DB and populate a DOM element, like so:

<h1>TOTALS: <?php fetchTotals(); ?></h1>

after submitting data, via JS-handled form, i want to fire that PHP function again to update the totals and not repeat code.

i have read this and this to see if there was anything useful there. and i already know i can just use $.ajax or $.post to run a file, but since the function already exists, and i hate repeating code, i wanted to know if there's a more elegant solution that obviates creating a file that exactly duplicates an already-extant function. follow me here? DRY, good; duplicate, bad.

Again: looking for more elegant way, not a basic how-to.

if not, then that's just the beast and screw DRY. but i want to be better. :)

Mulan
  • 129,518
  • 31
  • 228
  • 259
WhiteRau
  • 818
  • 14
  • 32
  • So just include your existing function from whichever PHP script is handling the form, and run it from there. You don't need two copies of it. – iainn Nov 07 '17 at 17:38
  • 1
    What code have you actually repeated? If your page includes `functions.php` and calls a function, why can't another page include `functions.php` and call a function? It sounds like you're trying to squeeze every possible keystroke you can out of your code, why? – David Nov 07 '17 at 17:38
  • Those answers aren't telling you to duplicate your code. If you have code that needs executing from several different sources than it should be extracted from where you currently have it and put in a central file. – Patrick Evans Nov 07 '17 at 17:45
  • @iainn code sample to illustrate? not sure what you're trying to say here. – WhiteRau Nov 07 '17 at 23:22
  • @David the `fetchTotals()` function would need to be repeated. – WhiteRau Nov 07 '17 at 23:23
  • @PatrickEvans i didn't want to have to make several AJAX calls just to do a simple PHP function call. seems, well, i dunno... stop making sense! :D – WhiteRau Nov 07 '17 at 23:24
  • @WhiteRau: I don't see how simply calling a function more than once is "repeating code". The whole purpose of encapsulating code within a function is to be able to easily invoke it in multiple places. I guess if you really want the function to only ever be called from one place then you could remove it from the initial page load and just make AJAX calls for both the initial and the updated value. – David Nov 07 '17 at 23:48

1 Answers1

1

Consider a dispatcher file -- essentially just a php file that grabs a param you pass to it (dispatcher.php?action=fetchTotals for example) that acts upon the action and calls that named PHP method. It could be just a simple php switch statement even.

Yes, it's an additional file. That being said, you're not re-writing or duplicating any methods. And, from a maintainability standpoint, if you have all your ajax calls going to a centralized dispatcher, it eases both readability and reuse. As an extra bonus, you could code it such that a call to a particular named method could actually call multiple PHP methods.

bpeterson76
  • 12,918
  • 5
  • 49
  • 82
  • If you go this route, you should whitelist the functions or put all the functions you want to call into a specific scope so you don't allow any old functions to be called (i.e. dispatcher.php?action=enableAdminMode) – Cave Johnson Nov 07 '17 at 17:50
  • okay. so instead of a dedicated file for each AJAX call, just call the functions page and push the 'action=doThisFunction' to it... dang. how did i not know about this? :D thank you! @KodosJohnson, thanks for the whitelist reminder too. – WhiteRau Nov 07 '17 at 23:21
  • i guess the thing that irritated me was that the function was right there to be called...it just had to be triggered. and now all this friggin' hoopla just to do exactly that. blech. – WhiteRau Nov 07 '17 at 23:33