PDA

View Full Version : Can you insert a php script into a HTML document?


XtremeGamer99
07-01-2004, 11:43 PM
This is going to be a pain to explian, But I'll try. What I want to do is is insert a PHP script into a HTML source code without converting the entire document to PHP. Like, I have a HTML page (with .html extension), and I want a PHP script on there, say a live clock (I know you can get it in JavaScript, but this is just an example). From what I understand, in order to get a PHP script to work is to convert the entire source code to PHP, which is a problem, since I don't know PHP. Of course, I could be wrong. But, I was thinking that you could insert oneinto an html sourc code by the <SCRIPT></SCRIPT> tags. Can you? If not, how can you do that?

Thanks in advance... :)

Spookster
07-01-2004, 11:55 PM
It is possible to tell the webserver to parse .html files as PHP files but it is really not necessary in your case.

I think maybe you should read about what PHP is and how it works.

http://www.php.net/manual/en/introduction.php

Read that and you will have the answer to your question.

Lamper
07-02-2004, 12:00 AM
Of course you can do this.

Change the file extension to .php
Dont worry it wont change the HTML part and inside the HTML whereever you want the PHP add it, the final doucment should look like this:

Filename: File.php


<html>
<head>
</head>
<body>
<?php

echo 'PHP code goes here';

?>
</body>
</html>

XtremeGamer99
07-02-2004, 12:17 AM
Of course you can do this.

Change the file extension to .php
Dont worry it wont change the HTML part and inside the HTML whereever you want the PHP add it, the final doucment should look like this:

Filename: File.php


<html>
<head>
</head>
<body>
<?php

echo 'PHP code goes here';

?>
</body>
</html>


Yes, but will that effect any of my html? Because I tryed to do that before, and didn't work.
Maybe I can better explain it if a give an example (and I'm not saying your wrong, I just want to make sure...)


<html>
<head>
</head>
<body>
<table width="100%" border="1" bordercolor="black">
<tr>
<td>This is HTML</td>
<td>This is html</td>
</tr>
</table>
<table width="100%" border="1" bordercolor="black">
<tr>
<td> in the next table cell, there should be a live clock</td>
<td>
<?php

echo 'Live clock PHP code goes here';

?>
</td>
</tr>
</table>
</body>
</html>

So, will something like that work? And all I need to do is change .html to .php?

XtremeGamer99
07-02-2004, 12:18 AM
It is possible to tell the webserver to parse .html files as PHP files but it is really not necessary in your case.

I think maybe you should read about what PHP is and how it works.

http://www.php.net/manual/en/introduction.php

Read that and you will have the answer to your question.
Thanks. Now I know where to go to acctually learn it. To bad I don't have time to check it out more today. And what do you mean when you say:
"It is possible to tell the webserver to parse .html files as PHP files but it is really not necessary in your case."

boeing747fp
07-02-2004, 03:25 PM
you can set up a MIME Type or Apache Handler to tell the server that .html files are actually php files... but like he said, you dont really need it

gsnedders
07-02-2004, 03:30 PM
Can't you do this: <script type="text/php">
echo ('Your PHP Script');
</script>

boeing747fp
07-02-2004, 03:42 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<script type="text/php">
$echo = date("d");
if($echo < 10){
$echo = str_replace("0","",$echo);
}else{
$echo = date("d");
}
echo date("l, F ".$echo."S, Y");
</script>

</body>
</html>

that didnt output anything for me on a .html or a .php page for me... but if i did.... <?php
$echo = date("d");
if($echo < 10){
$echo = str_replace("0","",$echo);
}else{
$echo = date("d");
}
echo date("l, F ".$echo."S, Y");
?>

.........it works for me on a .php page

LynxGrr
07-02-2004, 04:53 PM
Another option is like this. If the file you want to include the php script in is called index.html, change the file extension to .php, then reference to the other PHP file like so...

include('otherfile.php');

If your trying to avoid moving your main site (as in the site where you keep your equivalent of index.html) to a PHP enabled server tho, this wont work, as the main document itself is a PHP file.

If your doing something as simple tho, such as the clock script you mentioned, wouldnt it be best to save the hassle and just use Javascript? What is it specifically you are trying to accomplish?

litebearer
07-03-2004, 02:19 AM
As for the index.html, if one is unable, for whatever reason, to make the server parse html files as php, simply make the index.html a very simple file whose only purpose is to redirect the browser via a meta tag refresh to a file named index.php. That is imho the easiest minimal knowledge required solution.

colabus
07-03-2004, 08:01 AM
You can set whatever htm,html,php,php3 etc in the httpd.conf file of what is to be parsed as php. As for "live" i'm thinking you mean always current to the client in which case you can't, as PHP is server side code.

dimkafr
01-31-2005, 12:03 AM
That's really unbelievable, that a lot of people here can not simply read a question.

The idea to put a php script into html is turned to its subsitute: to use php on the page with .php extension. Fantastic! What an answer! It's annoying:-(


I'd like to get this how it is possible to insert a php script with <script> tag into a HTML page and I found this forum.

Perhaps somebody knows how to do it WITHOUT <?php ?> parts, which will NEVER work in the file with .html extension.

I'd be very happy to know it.

Unfortunately <script type="text/php"> doesn't work.

Can anyone help me?

Thanks a lot!

Spookster
01-31-2005, 12:14 AM
That's really unbelievable, that a lot of people here can not simply read a question.

The idea to put a php script into html is turned to its subsitute: to use php on the page with .php extension. Fantastic! What an answer! It's annoying:-(




It's normally not a good idea to ask for help by insulting the people you are trying to get help from. :rolleyes:

cfc
01-31-2005, 01:22 AM
Likewise, it really is a good idea to actually read and make an attempt to understand the content of the posts without making incredibly ignorant statements.

Here's what has been said so far, in case anyone hasn't caught it properly:

Because the server parses PHP before the browser gets the HTML (the browser does not see the PHP) you will need to put the PHP code or whatever PHP code includes it in a file that is parsed for PHP. As Spookster said, you can mark .html files to be marked for parsing, but it's not necessary and not efficient. If you have index.php in your DocumentIndex variable in httpd.conf (if you're not running the server and PHP is installed, this is probably true), simply rename index.html to index.php. Once it is renamed to index.php (or whatever other solution you come up with) the following simple code will include any PHP (or HTML, for that matter) file that you specify:
<?php
include("clock.php");
?>

Given the above, to answer this topic's question:
You can use the <script> tag with the language="php" attribute, as described here:
http://ca3.php.net/language.basic-syntax
but the <?php ?> tag is far more common and functionally identical, as the server will parse the contents of the script tag for PHP and remove the contents (and probably the tags too) before the output gets to the browser and even if you use the <script> tag, you still have to mark the page for PHP parsing by the server.

webtrek
01-31-2005, 02:53 AM
Wow, I am prepping to add a prebuilt PHP shopping cart to my website. I know nothing about PHP, except that I just got it to run locally on my mac after 2 days of confused: ...I look forward to rereading this post as I start to reskin the shopping cart...

Taylor_1978
01-31-2005, 03:47 AM
Likewise, it really is a good idea to actually read and make an attempt to understand the content of the posts without making incredibly ignorant statements.



Unless I am missing something - I didn't realise misunderstanding a question and answering the question incorrectly due to this was classed as an incredibly ignorant statement.. At least people were trying to help! And if wrong, so be it - better than no response at all in my opinion!

Trial and error! That's how we learn, we aint all experts and sometimes we think we know when we are wrong, that's life! But certainly no excuse to be rude about it, as one day - the people you shoot down will know the answer - and wont bother replying!

cfc
01-31-2005, 04:41 AM
Unless I am missing something - I didn't realise misunderstanding a question and answering the question incorrectly due to this was classed as an incredibly ignorant statement.. At least people were trying to help! And if wrong, so be it - better than no response at all in my opinion!

I was referring to this:

The idea to put a php script into html is turned to its subsitute: to use php on the page with .php extension. Fantastic! What an answer! It's annoying:-(

I know Spookster already said something about it, but I felt offended by it as someone that tries to give help every once in a while, and I was already posting to try to help clear things up, so I figured I might as well say something ;) . Valuable advice has been offered throughout this topic, and making an attempt to insult the people that have been giving this advice without bothering to take the time to understand it is ignorant. There is a huge difference between making a sarcastic and insulting response and admitting that you simply don't understand the advice given, though I believe you thought I was referring another post or several other posts, which I certainly wasn't as they were all completely valid :) . I wasn't trying to say anything against the people that needed help, either... just saying that it's best not to insult the people you're requesting it from if you want your request to be given a good amount of attention.

What I wrote before (if you got past the "ignorance" part) was more of an attempt to combine and reiterate the helpful advice given to see if I could help clear it up. IMO this topic has been solved several times already, as it really doesn't get much more simple than what I posted...

Taylor_1978
01-31-2005, 05:09 AM
As I stated, unless I missing something... which I did LOL - Totally misunderstood, sorry cfc. I'll go crawl back into my corner now!

marek_mar
01-31-2005, 07:34 AM
Hot topic this turned out to be eh?
PHP:
http://www.php.net/manual/en/language.basic-syntax.php
Apache:
http://httpd.apache.org/docs/mod/mod_mime.html#forcetype
http://httpd.apache.org/docs/mod/mod_alias.html#redirect

joeyb1234
05-11-2009, 11:51 PM
<?php
include("lib/vars.inc.php");
include("lib/db.inc.php");
include("lib/kernel.inc.php");
include("template/template.inc.php");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$QUERY = $_POST;
}
else {
$QUERY = $_GET;
}

extract($QUERY);

make2mid();
mysql_select_db($dbName);
$query="SELECT img_links.part_id, img_links.link FROM img_links WHERE img_links.img_id=$img_id";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$part_id=safestr($row['part_id']);
$img_link=safestr($row['link']);
$query="SELECT * FROM img_parts WHERE img_parts.part_id=$part_id";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$name=$row['name'];
$keywords=$row['keywords'];
$title=$row['descr'];
$TEMPLATE['KEYWORDS'] = $keywords ;
$TEMPLATE['DESCRIPTION'] = $title.". ".$TEMPLATE['DESCRIPTION'];
$part_link=safestr($row['part_link']);
$TEMPLATE['DATA'] .= " <table width=100% border=0 cellspacing=2 cellpadding=0>
<tr>
<td width=300 height=225><img src=".$part_link."images/".$img_link." width=300 height=225 border=1 alt=\"".$name." wallpaper\"></td>
<td rowspan=2 align=left valign=top>
<p>
<font color='#ff0000'><b>New!</b></font> <a href='http://download.freeze.com/lm/affiliate_downloads/samplewpaper_9767.exe'><b>Download a Free Wallpaper Pack!!!</b></a>
</p>
<b>How to use:<br>
</b><br> Wait until the picture has finished loading!<br><br>
<b>Windows Users (Windows 98/2000/XP):</b><br>Right click on the desktop picture and select 'Set as Background'
or 'Set as Wallpaper'. If you want to save it to your hard
disk just select 'Save image as...'
<p><b>Mac Users (For MAC O/S 8.0 and higher):</b><br>
Wait until the movie picture has finished loading, save the
image to your hard disk and use the control panel to set the
picture as your wallpaper. <br>
</p>
</td>


Here is where I want to place the Google script but it's not working? It gives me a blank page when I post it to the site...

<td width=50%>
<script type="text/javascript"><!--
google_ad_client = "';
/* 300x250, created 5/7/09 */
google_ad_slot = "";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


</td>

</tr>
<tr>
<td width=300 align=left valign=top><img src=images/!.gif width=16 height=16 border=0><font color=#ff0000><b> This is reduced image, just follow 'How to use' instructions to set wallpaper.</b></font></td>
</tr>
</table><br><br><br><br>";
makeaftermid();
makePage($title);
?>

timgolding
05-12-2009, 12:08 AM
Can't you do this: <script type="text/php">
echo ('Your PHP Script');
</script>

No lol. php is server side. The script tag is html and is for including client side scripts like javascript.
Though as cfc pointed out you can use


<script language="php">
echo 'some editors (like FrontPage) don\'t
like processing instructions';
</script>

But would still need to go inside a php file or run through php which sits on the server. So that code would still never get sent to the browser. But there is no way to get a browser to interpret php. Simple rule

PHP runs on server
HTML/Javascript runs on the client

Those rules have never let me down.

Flash is another story of course.

venegal
05-12-2009, 12:36 AM
No need to answer, this is over 4 years old.

timgolding
05-12-2009, 12:26 PM
oh lmao