Calling php from JavaScript -> can not find php-functions from other file

formi
formi used Ask the Experts™
on
Hi all,
if I call a php with a JavaScript  

window.location='path1/file1.php'

and call a fuction (with php) in that file1.php (i.e. <? myFunc1(); >? then this function can not be found. What have I to? Thanks in advance, Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Zach ShaverTechnical Lead
Top Expert 2012

Commented:
Well, if the sole contents of file1.php is <? myFunc1(); ?>, it definitely will not work.
myFunc1(); does not exist as a function in PHP so before you use it, you have to define it

Also, never use <? as an opening PHP tag as this is disabled by default on most PHP configurations. Always open with <?php.

you can definne the function before or after it is called, but it must be within the scope of the script or you must include it from another file
<?php
myFunc1();

function myFunc1() {
}
?>

or

<?php
function myFunc1() {
}

myFunc1();
?>

Commented:
Where is myFunc1 defined?

Author

Commented:
yes I know I just didn't write it: I defined the function in another php but it can not find that file. Meanwhile I made the following test:

I wrote a test.php that lists a certain directory:

if ($handle = opendir('backoffice/core')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

if I execute this php from the root-directory it shows me the files. If I execute it from any subdirectory it doesn't find any file. I think my problem has to do with this behavior.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
What does your Javascript look like when executing from the subdirectory?
Zach ShaverTechnical Lead
Top Expert 2012

Commented:
if you defined the function in another php script you must include it in the one you are referring to

e.g. if javascript is redirecting to file1.php, file1.php must:

include('file2.php');

in order to be able to access the functions within file2.php

Author

Commented:
sorry, by the test above I didn't call it with JS, I just entered test.php (works) or subdirectory/test.php (doesn't work) in the browser
Technical Lead
Top Expert 2012
Commented:
also, test.php is accessing the directory using relative addressing

it will only work in a folder that contains a backoffice folder, and if the backoffice folder contains a folder called core

if you want it to work in any folder you need to refer to it using absolute address like "c:\www\backoffice\core" or "/var/www/backoffice/core" depending on if you're doing this on a windows box or unix/linux box

but really this is another question you should have posted

Author

Commented:
I just tried it with c:/www/... (it's a windows-server) but no files are listed. You are right it seems to be a different question but I think my problem is in relation with that. The file where my function is defined is not found by the system. I try to describe it again:

in JS I call

window.location='path1/file1.php'

I have another file, file2.php with function xy()

in file1.php I want to call this function xy (calling with php) but I get the message "Fatal error: Call to undefined function xy"

Now I tried to include the file with include(path/file2.php). Here I get the error "Warning: include(): Failed opening '/path/file2.php' for inclusion"

So I tried to list the files to see which files are seen and had the problem above: no files are listed
Zach ShaverTechnical Lead
Top Expert 2012

Commented:
you are messing up your paths again

ummmm... c:\www was an example

and you are again confusing relative with absolute paths when including

if you begin the path with /, it assumes it is the root of the filesystem on unix/linux
if you want to include a file that is in the same folder it would be include('file.php');
if you want to include a file in a subfolder it would be include('subdir1/file.php');

if you want to include a file based on absolute path, you need to determine where exactly your script is located on the system before you can come up with the path... again c:\www was a example only, it depends on where your files are located.
Zach ShaverTechnical Lead
Top Expert 2012

Commented:
Let's say I have index.php located at c:\inetpub\wwwroot\index.php
another file with my function in it  at c:\inetpub\wwwroot\sources\classes\class.milkCow.php

in index.php, you can include the file in one of two ways, absolute:
include('c:/inetpub/wwwroot/sources/classes/class.milkCow.php');

or relative
include('sources/classes/class.milkCow.php');
this path is relative to the location of the script, index.php
Most Valuable Expert 2011
Top Expert 2016

Commented:
Looking at the original question, I see this:
<? myFunc1(); >?

What that is probably supposed to say might be something like this:
<?=myFunc1(); ?>

The shorthand notation using <?= is equivalent to writing <?php echo.  In the example that has <? but not <?= the function will run but PHP will not produce its output.  Not sure if that is the problem, but it looks like it might be.  Give it a quick try and see if you get something more like what you expect.

best to all, ~Ray
Zach ShaverTechnical Lead
Top Expert 2012

Commented:
if that were the case ray it would not say it was an undefined function. he hasn't defined it yet or isn't including the files that contain the definition
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is an organizational strategy that might be helpful.  Put all of your classes and functions and common initialization programming into one single PHP file and include it at the top of every PHP script.  The common file is where you would put date_default_timezone_set(), session_start(), the data base connection and selection, etc.  Put error_reporting(E_ALL) into this script.

Then make the first line of every script something like this:
<?php require_once('common.php');

If you have a lot of sub-directories, you might want to look at <?php phpinfo(); to see if there is a $_SERVER["DOCUMENT_ROOT"] defined.  You may be able to use that to point your sub-directory scripts to the correct common.php

Author

Commented:
I really had the problem that including the file didn't find it and with the absolute path it works (and of course you are right, the path wasn't c:/www as in your example. Now it works. Thanks to all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial