Page 1 of 1
calling PHP in HTML page
Posted: Fri Dec 15, 2006 11:45 pm
by sandeep_from
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I have a .html page.
I need to call an output of a PHP code in it.
Code: Select all
<?php
// Lookup MySQL
echo $result_of_mysql;
?>
I believe inserting the above php code in a .html page will not work.
So I need to put this code in an expternal file and call it in the HTML page.
How should I call an external PHP script in a .html plage.
Thank you.
cheers,
Sandeep
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Dec 15, 2006 11:50 pm
by feyd
You can just link to it, you can stick it in a (i)frame or you can request it via Ajax (but that isn't just html.)
Posted: Fri Dec 15, 2006 11:52 pm
by Zoxive
The easiest way would be rename it to .php
Or you need to tell php to "look" at html files and parse them.
The easiest way to do that is edit your .htaccess file, and add something like..
Code: Select all
RemoveHandler .html
AddType application/x-httpd-php .php .html
Posted: Sat Dec 16, 2006 12:03 am
by sandeep_from
I think there is some way to call it using Javascript; external script call.
Not used and not sure how to code it.
regards
Posted: Sat Dec 16, 2006 12:04 am
by John Cartwright
sandeep_from wrote:I think there is some way to call it using Javascript; external script call.
Not used and not sure how to code it.
regards
As feyd mentioned, it's called Ajax. But why can't you just change the file extension or use Zoxive's suggesion?
Posted: Sat Dec 16, 2006 12:15 am
by sandeep_from
I will then have to change the file name in 100s of HTML pages where this page is called.
Too much trouble.
Also change in page extension will effect Google indexing.
I just need to call one value from a MySQL. so I guess I will search the javascript code (Ajax) and try it that way.
Suggestions / pointers welcome
Cannot change Apache handlers. (I believe this wa sone of the suggestions). the page is on a shared host.
Sandeep
Posted: Sat Dec 16, 2006 12:19 am
by John Cartwright
sandeep_from wrote:Cannot change Apache handlers. (I believe this wa sone of the suggestions). the page is on a shared host.
You make the changes in your .htaccess file. Not into your httpd.conf.
Posted: Sat Dec 16, 2006 3:59 am
by neel_basu
You Can Do This
Zoxive wrote:The easiest way would be rename it to .php
Or you need to tell php to "look" at html files and parse them.
The easiest way to do that is edit your .htaccess file, and add something like..
Code: Select all
RemoveHandler .html
AddType application/x-httpd-php .php .html
But Most Of The Hosts Parses html Files As php
Posted: Sat Dec 16, 2006 4:10 am
by volka
Must have been other providers than I've tested

Posted: Sat Dec 16, 2006 4:15 am
by m3mn0n
sandeep_from wrote:I will then have to change the file name in 100s of HTML pages where this page is called.
Too much trouble.
Also change in page extension will effect Google indexing.
I just need to call one value from a MySQL. so I guess I will search the javascript code (Ajax) and try it that way.
Suggestions / pointers welcome
Cannot change Apache handlers. (I believe this wa sone of the suggestions). the page is on a shared host.
Sandeep
This is why I love clean URLs (Apache's mod_rewrite)
You can have a section such as "mysite.com/sitelist/autos/" and at first if you want to keep it a simple HTML page but later want to switch to have dynamically generated list and a PHP page, your URL doesn't change one bit. And this has other benefit such as links to your site aren't broken, search engine rankings stay the same, and etc.
Not mapping your URLs to physical parts on the server filesystem, & keeping your URLs technology independent I think is smart and sensible for the above reasons. Plus another ability is for you to alter file extensions and have the old ones simply map to the new ones.
So you can go ahead and change it to .php, tell mod_rewrite to point calls for the .html file at the .php file, and your set. It's a bit of an ugly hack IMO (avoidable in the future by simply keeping clean URLs in mind during application development), but it'll do the trick.
Just about every mod_rewrite beginner guide on the web is horrible and you'll have a difficult time with it at first, but once you pick it up it's a breeze.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Another solution I'd recommend is, as mentioned, Ajax.
Posted: Sat Dec 16, 2006 8:20 am
by RobertGonzalez
You could always create a PHP page that does what you want and use a meta redirect from the static html page to new PHP page. It is a potential performance hit, but you wouldn't have to change any page names.
I would also suggest on your next site update that you do away with file name extensions in your links altogether and go with URL rewrites to achieve what you want. It makes it a lot to maintain your site in the future. I think m3mn0n had suggested that a post or two above this one.