PDA

View Full Version : How to link javascript file with php file?


registereduser4
10-22-2009, 04:56 PM
I have HTML file and a separate javascript file js.js. I link js.js from HTML file by put following scripts in HEAD of HTML:
<script type="text/javascript" src="js.js"></script>

It works fine. Now I would like to add a PHP file for purpose to get data from MYSQL database. I wish to use php to retrieve a table, then turn it into an array which is to be used by the js.js file.

My question is how do I link js.js and php.php? Since retrieving action is initiated by js.js and the array is to be used by js.js, I guess I need to put something as follows in js.js rather than in HTML file.

<script type="text/javascript" src="php.php"></script>

How do I do it? Thanks.

abduraooft
10-22-2009, 05:01 PM
http://codingforums.com/showpost.php?p=718591&postcount=4 may help

registereduser4
10-22-2009, 06:24 PM
http://codingforums.com/showpost.php?p=718591&postcount=4 may help

Thanks a lot for reply. Where should I put following scripts, in HTML or js.js?

<script type="text/javascript" src="myfile.js.php"></script>

Or, are you saying that I do not need a separate .php file, but put php scripts into javascript file and change the name of js.js to js.php?

jolly_nikki
10-22-2009, 06:26 PM
You need to add in html file, within the head tag
<script type="text/javascript" src="myfile.js"></script>

registereduser4
10-22-2009, 06:32 PM
You need to add in html file, within the head tag
<script type="text/javascript" src="myfile.js"></script>

Thanks jolly_nikki. I believe now it would be myfile.php or myfile.js.php. Does ".js." play any role here?

registereduser4
10-22-2009, 06:43 PM
So, there will be only one scripts file with tension of .php? Am I right?

What about if I prefer to have a separate .php file? I indeed have another question, if I have two .js files, how to call functions located in other .js file?

jolly_nikki
10-28-2009, 11:29 PM
<script type="text/javascript" src="myfile.js"></script>

<script> tag is used to include only javascript files not php or any other.
If you want to use two .js file then add another script tag.

<script type="text/javascript" src="myfile1.js"></script>
<script type="text/javascript" src="myfile2.js"></script>