"include" comand

+2 mike dunne · November 23, 2014
does the "include" command only work in the index file or can I use it in any file.. because in my register.php file I use the include 'form.php' ; it doesn't work but when I open the form.php file it it works on its own... it just wont work in register.php

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Ron Butcher · November 25, 2014
I just saw you had two posts about this so ignore my other one.  I ran the code you give here, and it does work as long as both files are in the same folder.  If they are in a different folder, you need to add either an absolute or relative path.

I do have some suggestions though to clean it up.  Include takes whatever page you are including and adds it to the current document, then presents it to the browser as one document.  So when you do an include, you only want the code that needs to be inserted to show up instead of an entire new page.  In both of your pages, you declare the Doctype and HTML tags.  That is bad form and could cause odd issues.  You are also not closing all of your tags in the include file.

Here is a better way to include this form.  Done this way it can be re-used in different pages as well.

form.php:

	<div id="form-wrap">
<form action="contact.php" method="post">
<fieldset> <h4>Contact Us</h4>
<label class="labelone" for="name">Name:</label>
<input name="name"/>
<label for="email">Email:</label>
<input name="email"/>
<label for="comments">Comments:</label>
<textarea name="comments"></textarea>

register.php:

<!DOCTYPE html>
<html Lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
<title>Register</title>
<style type="text/CSS"> 
h1 {COLOR:blue;} h1 {font-family:"comic sans ms", cursive, sans serif;} 
H2 {background-color:yellow; border: 2px solid blue;} h4 {font:18px "comic sans ms", cursive, sans serif;} 

#form-wrap{width: 600px; border: 3px solid blue; background-color: lightblue; margin: 50px auto;
padding: 20px 20px;}

label{display:block; margin: 5px 0 5px 5px; font: 18px "comic sans ms", cursive, sans serif;} 
input{width:300px; margin: 5px;} 
textarea{width:300px; height:50px; margin: 0 0 0 5px;} 
fieldset{border:1px solid blue;}
.btn{width:120px;}
</style>
</head>
<body>
<H1 Align="CENTER"> LuckyPuppyOptions </H1> <!-- Header -->
<!-- 09 03 2014 -->
<H2 Align="CENTER"> Register </H2>
<hr /> <!-- line -->
<p align="center"><a href="index.html">Home </a>

<?php include 'form.php'; ?>
</body>
</html>

In the include, the only thing that exists is the form it self.  No other information.  This allows you to easily use it from page to page if needed.
0 Andrew McCombs · November 25, 2014
I have had some issues on some files not including. Here are your options.

include 'form.php';
include '/form.php'; <- meaning root Dir.
include '../form.php'; <- meaning if the file is not in the root Dir ( ../ ) for each folder

if that doesnt work try this.

<?php

$path = $_SERVER['DOCUMENT_ROOT'];
include $path . '/form.php';

?>
  • 1
  • 2

PHP

126,363 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators