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
formiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZShaverCommented:
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();
?>
0
gr8gonzoConsultantCommented:
Where is myFunc1 defined?
0
formiAuthor 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.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gr8gonzoConsultantCommented:
What does your Javascript look like when executing from the subdirectory?
0
ZShaverCommented:
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
0
formiAuthor 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
0
ZShaverCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
formiAuthor 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
0
ZShaverCommented:
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.
0
ZShaverCommented:
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
0
Ray PaseurCommented:
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
0
ZShaverCommented:
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
0
Ray PaseurCommented:
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
0
formiAuthor 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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.