-1

I am including 3 different PHP files on a page index.php using ajax on 3 different button clicks, but all these PHP files further requires another PHP file, lets say it a.php

So in this case I've included a.php in all three files I'm calling with ajax

Including a.php in all three files is not the right way to do it, I think

How can I make a.php included only once so that it works with all three?

Rajesh Kumar
  • 17
  • 1
  • 6

3 Answers3

0

Let PHP decide to include or not:

include_once()
require_once()

Check the function reference which fits your needs better.

JustAPirate
  • 164
  • 5
0

PHP start new process for each call (AJAX or not), so each time any of this files are called by AJAX thy need to include everything they need to operate. To make sure that You don't include one time several times for one call use require_once() or include_once()

StormRideR
  • 1,738
  • 1
  • 14
  • 13
0

Apologies if I'm misunderstanding - You have something like:

Ajax call 1: file1.php - output response to page

Ajax call 2: file2.php - output response to page

Ajax call 3: file3.php - output response to page

All 3 of those files need the functionality in a.php to run, correct?

Could you instead combine the 3 files into 1 PHP file, with a.php included, and then pass them whatever parameters are specific to each button click? So you'd have:

Ajax call 1: bigfile.php?parameter_blah=a

Ajax call 2: bigfile.php?parameter_blah=b

Ajax call 3: bigfile.php?parameter_blah=c

Again, apologies if I've misunderstood.

pnellesen
  • 81
  • 4