Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling PHP Function from within HTML file? 1

Status
Not open for further replies.

tyhand

Programmer
Jul 3, 2002
186
0
0
US

Hi all,

Still a php newbie after all this time.

I'm wondering; Can I call a PHP function from within an HTML file? (the php file with the function would exist in another folder)

Example: myFolder/myFile.HTML has some php code...
<?php
myFunction()
... yada yada
?>

I would like to call myFunction from within myFile.HTML.
Possible?

Any and all help, leads, wisdom and knowledge is
greatly appreciated.

Thanks!
-tyhand
 
Under certain circumstances it is possible, however, under normal circumstances it is not.

You have to setup your server to parse html files through PHP so it executes the php file code in the html file. However this means that every html fle regardless of wether it contains PHP code or not will be parsed through PHP. This may or may not be what you are after, and it might slow down the server when serving lots of pages or pages with a lot of information but no actuall PHP code to process.

Hope ths helps

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yes, if you tell the php module (I think it's in the php.ini file... but don't quote me on that) to treat .html extensions as a php file. However, I would advice again such an action. There really is no difference between a php page and a html except that you *can* (not you use) put php code in one and not the other. If you're trying to make it you don't have to rename all your html pages to .php, I would say just do it. It's better to have a logical strucutre than an doppleganger of a logical structure.
 
Hi all,

Yes, you are both right!
I actually got a few good tips a couple of months back about changing the php.ini file to parse html pages
(all right here in the priceless forum).

I'm wondering about the methodology though.
Would an include() statement be the right
way to go to call the function from within the html file?

I want the function to be called when the myFolder/myFile.HTML page loads. I don't think php
has an onload() function like javascript.

Your thoughts?

Thanks again. Peace!
- T Y H A N D
 
If your host is apache, and you can use .htaccess files, for a given directory you can make apache parse html with php for just that directory, which is alot less overhead than doing the whole site.

.htaccess
AddType application/x-httpd-php .php .html

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
The Include should work, it will get executed wherever the include is, so if it is placed at the top, it will get run at the beggining of the file, so if you have functions you need to use later, they will be available.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top