InMotion Hosting Support Center

In this tutorial series, we're creating a simple 2 page website in which each page allows users to post comments. In our previous articles, we've created and setup our database, and we've created the html form that will allow users to type in and submit their comments. In this article, we now need to incorporate our HTML form into our 2 pages. This can be done using the Include php function.

Creating our Test website

At this point in our tutorial, we need to create the 2 pages that will appear on our website. As the purpose of this tutorial is not to show you how to create a basic website using HTML, we won't go through the process of creating those two pages. We've created the two pages (page1.php and page2.php) and they can be seen below:

page1.php page2.php
this-is-page1 this-is-page2

Adding our HTML form to our website using PHP's include function

In our previous article, we created an HTML form. Because we only have 2 pages, we could copy and paste the same code onto each page. In this example, that could be OK. If we however had 100 pages, that would be quite a bit of work. Also, if we had to make a change to that form, we would need to make that change to 100 different pages, which would be very tedious.

To make this task easier, we will add our HTML form to a file called formcode.php and then include that file in each of our pages. If we go this route, if we needed to make a change to the form in the future, we would simply change the one formcode.php file and the change would affect all pages that included formcode.php.

To setup formcode.php and include it in your pages:

  1. create a new file named formcode.php and insert our form code (found in our previous article)
  2. add in both page1.php and page2.php where you would like the form to appear.

After following the steps above, your pages should look similar to the below screenshots and your code should look similar to the code sample below:

page1.php page2.php
page1-with-the-form-added page2-with-the-form-added

This is page1.php

Click here to go to page2.php

In our next tutorial, we'll show you how to process the user's comment after they have clicked the submit button to submit their comment.

Was this article helpful?
Continued Education in Course 205: Using PHP to create dynamic pages
You are viewing Section 4: Using the php Include Function to Reuse Code
Section 3: Creating a simple HTML Form to get user comments
Section 5: Using PHP to INSERT Data into a Database

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Forum Login

You are NOT logged in. You can still browse our Support Center.

To participate within our Community Support Forum:

n/a Points
2017-04-21 1:00 am

Hello, Thank you for the great effort and article and follow up.

I've followed your examples but I have faced some confusions with the pages, but at last I've got 4 pages, my problem in the pages is : 

what is working : the colors and links to each other and the ID on the address 

what is not working :  it doesn't show the form !!

page 1 & 2 contains the same code (just change page ID) :

<? include("manage_comments.php"); ?>
<h1>This is page1.php</h1>
<div><a href='page2.php?id=2'>Click here</a> to go to page2.php</div>
<div style='margin:20px; width:100px; height:100px; background:blue;'></div>
<? include("formcode.php"); ?>


Formcode.php contains :

<form method='post'>
  NAME: <input type='text' name='name' id='name' /><br />
  Email: <input type='text' name='email' id='email' /><br />
  Website: <input type='text' name='website' id='website' /><br />
  Comment:<br />
  <textarea name='comment' id='comment'></textarea><br />
  <input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />
  <input type='submit' value='Submit' />

Thank you

2,858 Points
2017-04-21 11:24 am
Tom, I would recommend that you ensure that your "formcode.php" file is in the same folder as the page that you're including it on. Also, I would check and make sure that the permissions and ownership for the file are correct. Finally, I would recommend <a href="">checking the PHP error log</a> for any related errors.
n/a Points
2015-08-21 11:07 pm

Although it sounds like a dumb question, but how do i assign id=1 and id=2 to the url for both pages

43,761 Points
2015-08-24 1:40 pm
Hello Andrea,

The id is added automatically by the database. You can see that from an article earlier in the series.

Kindest Regards,
Scott M
n/a Points
2015-03-16 7:02 am

I am an up and comming new business and this was very helpfull to understand who I need my domain to go through.

n/a Points
2014-12-31 12:13 pm

i have done all, i seen here, but still any time i submit a comment to mysql database, it redirect to another page saying connected successful !! the only time i see user comment on the webpage is when i go back and refresh the page. please i need help with this immediately thanks

42,946 Points
2014-12-31 12:25 pm
Hello Jerry,

Sorry to hear that you're having problems with the php Code provided above. The issue you're referring to should be addressed in the next article, How to process the user's comment after they have clicked the submit button. If you have followed those directions and you're still having issues, then please provide us more information (such as the code you're using), or at least a URL, so that we can observer what you have done and troubleshoot if there is an existing problem.

Arnel C.
n/a Points
2014-08-07 2:38 pm

I had to change this <? include("formcode.php"); ?>

to this<?php include("formcode.php"); ?>

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

8 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!