Link to home
Start Free TrialLog in
Avatar of formi
formiFlag for Switzerland

asked on

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

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
Avatar of Zach Shaver
Zach Shaver
Flag of Canada image

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();
?>
Where is myFunc1 defined?
Avatar of formi

ASKER

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.
What does your Javascript look like when executing from the subdirectory?
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
Avatar of formi

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Zach Shaver
Zach Shaver
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formi

ASKER

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
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.
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
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
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
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
Avatar of formi

ASKER

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