tags:

views:

116

answers:

5

I have a javascript file (.js file, not HTML file) containing several js functions. I want to call one of the PHP functions in a PHP file containing only several PHP function from within one of the js functions. Is that possible? Would I need to "include" the .php file containing the php function in the .js file? How would I do that? For example, say I had a file called myLib.php containing a function called "myFunc" that takes two parameters (param1 and param2). Then I have a .js file containing a function called "myJsFunc". How would a call the myFunc (PHP) from within the myJsFunc (javascript function)? Wouldn't I need to include the PHP file somehow in the javascript/.js file?

+2  A: 

PHP and JS are not compatible; you may not simply include a PHP function in JS. What you probably want to do is to issue an AJAX Request from JavaScript and send a JSON response using PHP.

nikic
+1 You don't even need a JSON response, it can really be anything; as long as you can parse/use it with JS in a meaningful way.
quantumSoup
+2  A: 

This is somewhat tricky since PHP gets evaluated server-side and javascript gets evaluated client side.

I would call your PHP file using an AJAX call from inside javascript and then use JS to insert the returned HTML somewhere on your page.

Josiah
A: 

You can't include server side PHP in your client side javascript, you will have to port it over to javascript. If you wish, you can use php.js, which ports all PHP functions over to javascript. You can also create a new php file that returns the results of calling your PHP function, and then call that file using AJAX to get the results.

CD Sanchez
A: 

Because the Javascript executes in the browser, on the client side, and PHP on the server side, what you need is AJAX - in essence, your script makes an HTTP request to a PHP script, passing any required parameters. The script calls your function, and outputs the result, which ultimately gets picked up by the Ajax call. Generally, you don't do this synchronously (waiting for the result) - the 'A' in AJAX stands for asynchronous!

Paul Dixon
Right...this is what I am doing. I am constructing the a URL calling a php function inside the javascript function that uses XMLHttpRequest. The question is can I call a php function inside of an included .php file or do I have to isolate the PHP code in a single file that contains the straight PHP code (no function) that the js code calls directly. I just don't want to have a bunch of PHP files with code in them sitting around. I'd like to incorporate the code in PHP functions in a .php file.
GregH
You could have all your functions in a single file then create a .php file that only has the include line to your functions file and a call to that function.
Josiah
@GregH What you want is to have an API that your JS function can call (via AJAX or what not), specifying functions and arguments. This API could be in a single file or multiple, it depends on what you want.
quantumSoup
Once you use AJAX to call the .php script on your server you can do anything in your .php script that you would normally do. There is no difference. It sounds like that is what is mixing you up. You can include any other script in the execution that you need to.
John
Also you can include arguments via post or get to specify what files you need to include in the .php function to only pull what you need.
John
Of course what @John said about specifying includes needs to be properly sanitized.
quantumSoup
+2  A: 

If you just need to pass variables from PHP to the javascript, you can have a tag in the php/html file using the javascript to begin with.

<script type="text/javascript">
    phpVars = new Array();
    <?php foreach($vars as $var) {
        echo 'phpVars.push("' . $var . '");';
    };
</script>
<script type="text/javascript" src="yourScriptThatUsesPHPVars.js"></script>

If you're trying to call functions, then you can do this like this

<script type="text/javascript" src="YourFunctions.js"></script>
<script type="text/javascript">
    functionOne(<?php echo implode(', ', $arrayWithVars); ?>);
    functionTwo(<?php echo $moreVars; ?>, <?php echo $evenMoreVars; ?>);
</script>
sgrif