Current location: Hot Scripts Forums » Programming Languages » PHP » including php file within html


including php file within html

Reply
  #1 (permalink)  
Old 06-12-06, 07:48 PM
albobis albobis is offline
Newbie Coder
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
including php file within html

How can i include a php file within html?

PHP Code:

<?php

// works
if ((include 'template_add_url.php') == 'OK') {
   echo 
'OK';
}
?>
This displays my html page as normal, but doesn't include the php file?

cheers

Last edited by nico_swd; 06-12-06 at 08:05 PM.
Reply With Quote
  #2 (permalink)  
Old 06-12-06, 07:52 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
it works like this...

PHP Code:

<?php

ehco
"I'm going to include template_add_url.php";
// works
if ((include 'template_add_url.php') == 'OK') {
include(
'template_add_url.php');
echo 
'OK';
}
?>
that should work.... b/c you just checked with an if statement and never actually included it... try that tell me if it works...

Last edited by nico_swd; 06-12-06 at 08:06 PM. Reason: [php][/php] tags added.
Reply With Quote
  #3 (permalink)  
Old 06-12-06, 08:05 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You cannot include files within HTML if it's not configured. You can either change the extension to .php, or you can use SHTML. (You would have to change the extension to .shtml though.)

Code:
<!--#include file="template_add_url.php" -->
Plus, include() will not return "OK" if it includes the file. Try this instead

PHP Code:

<?php

// works
if (include 'template_add_url.php') {
echo 
'OK';
}
?>
Reply With Quote
  #4 (permalink)  
Old 06-12-06, 08:17 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Beaten to the submit button again...

However, it is possible for an include file to return a value and have it tested. The code from the 1st post here is straight out of the PHP manual (and I just confirmed this works on my test setup.)

So, this should work, assuming the file is being parsed as PHP code (either a .php extension or that the .html extension has been added to the list of files parsed as PHP.)

If the include file is something like this -
PHP Code:

<?php

$a 
"this is a";
return(
"OK");
?>
and the code that includes it is this -
PHP Code:

<?php

if ((include 'template_add_url.php') == 'OK') {
    echo 
'OK<br />';
    echo 
$a;
}
?>
the resultant output will be -
OK
this is a


This assumes that the path to the include file is correct, along with all of the other things that could normally prevent this from working.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old 06-12-06, 10:15 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
The simplest thing to do is put this in your .htaccess file:

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

Now Apache will parse PHP code in .htm files just as if they were .PHP files.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I insert PHP code into HTML ?? cc79 HTML/XHTML/XML 5 04-12-08 11:00 AM
PHP - Unable to remove items from a file AdrianLewis PHP 5 03-17-06 05:14 AM
PHP to generate HTML as loaded from client side mgscom PHP 2 01-14-06 12:02 AM
Saving php generated file as html file GS300 PHP 0 12-29-04 03:34 AM
Can anyone help me ? (problem using php variables in html db insert code) chronic_ PHP 2 06-13-04 11:19 AM


All times are GMT -5. The time now is 07:17 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.