I'm almost going crazy here.
I have a simple site locally with a contact form functionality that works perfect with just PHP.
Now I want to call it via AJAX and I always get errors because of the relative paths.
The problem is, that the php file called by ajax has two require_once, which won't work then.
I know that I should use absolute paths to get it working and I tried different approaches suggested by different posts, but all result in an error.
I tried using
require_once __DIR__ . 'app/config/settings.php';
I used define('ROOT_SYS',dirname(__FILE__).'/');
in the index.php making the ajax call and having the require in the php file like this:
require_once ROOT_SYS . 'class/model/contactForm.php';
and many other approaches like recommended in this question PHP include best practices question, without success. I get them all to work php side, but when I use ajax it always results in an error like:
Notice: Use of undefined constant ROOT_SYS - assumed 'ROOT_SYS' in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Warning: require_once(ROOT_SYSclass/model/contactForm.php): failed to open stream: No such file or directory in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Fatal error: require_once(): Failed opening required 'ROOT_SYSclass/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
OR
Warning: require_once(app/class/model/contactForm.php): failed to open stream: No such file or directory in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Fatal error: require_once(): Failed opening required 'app/class/model/contactForm.php' (include_path='.;G:\xampp\php\PEAR') in G:\xampp\htdocs\website.de\app\class\controller\contactForm.php on line 6
Either a constant is undefined, because it wasn't included or opening a stream failed.
I mean there must be way to get it working with having a require_once
in the calles PHP file.
Do you have any experiences you can share with me? I'm really desperate right now.. :(
My folder structure is like this:
app
-class
--controller
--helper
--model
--view
-config
-template
docroot
lib
system
-css
-js
index.php
My project path on localhost:
G:\xampp\htdocs\website.de
http://localhost/website.de/
My ajax call
$.ajax({
type: "POST",
url: "app/class/controller/contactForm.php",
data: content
})
And the requires in the called contactForm.php how I had them from the start on.
require_once 'app/class/model/contactForm.php';
require_once 'app/config/settings.php';
There must be some way to get it working, preferably with a setup that works locally and on a webserver.
I know this is a lot to read, so thank you for your time!!
Using __DIR__
is the way to go. You should elaborate on that statement:
require_once __DIR__ . 'app/config/settings.php';
I guess you are just missing the /
because __DIR__
will give you a path without the trailing /
. It should be:
require_once __DIR__ . '/app/config/settings.php';