"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
+1 Bucky Roberts · November 23, 2014
It works in any file. Can you post your source code? I will try to find out why it isn't working. 
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';

?>
0 mike dunne · November 23, 2014
this is what I have so far but I thought it should work... I am just learning so go easy on me... I have been watching your videos and learned a lot.. did my first website thanks to you but now I want to register users and having a little bit of a problem.. trying to build a register page... I have a form that I made and I just want to use it at the top of the page to save some space and use the form for other things.. I have a form.php file that works just having a problem linking it to the register.php file...

<!DOCTYPE html>

<?php include 'form.php'; ?>
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>
0 Taf Shllaku · November 23, 2014
Try this:


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

</html>
0 mike dunne · November 23, 2014
this doesn't work also..

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

</html>
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 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 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
  • 1
  • 2

PHP

106,971 followers
About

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

Links
Moderators