calling PHP in HTML page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sandeep_from
Forum Newbie
Posts: 5
Joined: Fri Dec 15, 2006 8:04 am

calling PHP in HTML page

Post by sandeep_from »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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
sandeep_from
Forum Newbie
Posts: 5
Joined: Fri Dec 15, 2006 8:04 am

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
sandeep_from
Forum Newbie
Posts: 5
Joined: Fri Dec 15, 2006 8:04 am

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Must have been other providers than I've tested ;)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply