How do you call a JavaScript function from one PHP file that is defined in another PHP file?
views:
588answers:
3This is a best guess at what you want :)
I am assuming you have two php files, that end up being sent to the browser as html. The solution is to move your javascript function to an separate (external) javascript file and include them in both of the php files (not with the phps include. See below). That way you can call the javascript function from both files.
All you need to do is move the function to an new file, then put the link to the external javascript file in the head of the html code (in the php file)
<script type="text/javascript" src="js_src.js"></script>
Include the php file in which the javascript function is located in the file where you want to use the function..
for eg. file.php -->contain js function u want to use in file2.php
and in file2.php
include following line
include("file1.php");
then you will use that function easily
Hope this will help
Maybe I won't answer, but I'd like to clarify the question.
First of all, you can't call JavaScript function from PHP. Browser calls JavaScript functions. If you know that, earlier answers are fine. If not, I'll try to explain that.
PHP files are processed on a web server. They are parsed, compiled and executed. One of the possible results of executing PHP files is HTML code, which is sent to the browser. HTML may contain JavaScript. HTML and JavaScript are parsed/executed by browser.
Now, if you have a situation where two or more PHP files are generating single HTML page, to the browser it seems like a single file, so if the first PHP script generates some '' tags, every consecutive PHP code may generate HTML which employs that JavaScript.
HTH.