"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 1
Oldest  Newest  Rating
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';

?>
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 mike dunne · November 24, 2014
do I need another file to make the include work
0 mike dunne · November 24, 2014
the file name is form.php


I tried it the way you recommended also cant get it to work ive checked the path and location.. I put it in a file called register.php like this..

<!DOCTYPE html>
<html>
<?php include 'form.php'; ?>

</html>

the form works just not in the other file path and filename are good checked and rechecked... going crazy
0 Franz Schmidt · November 23, 2014
Tried your code and everything looks nice.
Really don't know what could be wrong but there only can be a misstake with the filename or the filelocation. :(
0 mike dunne · November 23, 2014
here is the form.php file
maybe there is something in here that wont let it work in the register.php file
it works fine from the form.php file....

<!DOCTYPE html>
<html Lang="en">
  <head>
       <meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
      
    <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>
 
 <title>Register</title>
 
  </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>
 
   <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">
0 mike dunne · November 23, 2014
this doesn't work also..

<!DOCTYPE html>
<html>
<?php include_once('form.php'); ?>

</html>
0 Taf Shllaku · November 23, 2014
Try this:


<!DOCTYPE html>
<html>
<?php include_once('form.php'); ?>

</html>
0 mike dunne · November 23, 2014
it is in the same folder as the register.php folder.

<!DOCTYPE html>
<html>
<?php include "form.php"; ?>

</html>
-1 Franz Schmidt · November 23, 2014
Make sure your filename is correct and also make sure you saved the form.php file in the right folder (may you need to add something to your include path).
  • 1
  • 2

PHP

107,041 followers
About

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

Links
Moderators