-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. :)

7
  • 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. Commented Nov 7, 2017 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? Commented Nov 7, 2017 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. Commented Nov 7, 2017 at 17:45
  • @iainn code sample to illustrate? not sure what you're trying to say here. Commented Nov 7, 2017 at 23:22
  • @David the fetchTotals() function would need to be repeated. Commented Nov 7, 2017 at 23:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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)
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.