In this tutorial series, we're showing you how to interact with a MySQL Database using php. We're creating a simple 2 page website that allows users to post comments on either page, and then shows all comments that have been posted. In our previous article we created a database and setup a table to hold those comments, and now in this article we'll setup a HTML form that allows a user to enter comments.

A quick introduction to HTML Forms

Gathering the Form Data

An HTML form allows a user to type data into a website, and then it submits the data to another page for processing. For example, if you fill out a comment on a website and hit submit, everything that you typed in is submitted to a script on the server that writes your comment to a database.

HTML forms can handle quite a few different forms of input. For example, they can handle:

  • Text Boxes:
  • Radio Buttons
    Option 1
    Option 2
  • Checkboxes
    Selection 1
    Selection 2
  • ... and more!

Submitting the Form Data

After the user has filled in the above data and clicks a submit button, it will send all of their data to another script for processing. It can either send the data back to the same page for processing, or it can send it to a completely different script to handle the processing.

When the data is passed to a script for processing, it can be passed using either GET or POST. When using GET, all the data is passed in the url, such as:
GET
domain.com/page.php?name=brad&email=brad@domain.com
As you can see, my name and email address is passed in the URL. If you use POST (instead of get), the URL will simple read:
POST
domain.com/page.php
... and my name and email address will be passed behind the scenes.

Our Example Contact Form

Because our comments form is going to be relatively simple, we will be using text boxes for most of the input. Below is our contact form and what the contact form will look like:

<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' />  
</form>

NAME:
Email:
Website:
Comment:

To keep our example straight forward, the form above does not include much formatting. In a real life scenario, you could use css and other html to elements to make the comment form a bit more user friendly.

When looking at the above form, pay attention to the following:

  1. In the form tag, we set the method to post (vs get).
  2. We have a hidden value that will hold the article's id. We need to know whether the comment is for page 1 or page 2. The value of that articleid will be grabbed using PHP and $_GET.
    For example, we'll have two pages:
    PHPandMySQL.inmotiontesting.com/page1.php?id=1
    PHPandMySQL.inmotiontesting.com/page2.php?id=2
    Notice in the URLs that one has id=1 and one has id=2. We will use PHP to grab this data so that we know which page the comment should belong to.

When the user fills out the information and hits submit, it will POST the data back to the same page. In our next article, we'll show you how to process that data and put it into the database.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?
n/a Points
2014-03-06 7:37 am

hallo, please help me, i get invalid article id, i already follow all the instruction and no such php?id=1 at my page, im using phpadmin v 5.1,

without article id, nothing i can progress. tq

n/a Points
2014-03-06 8:38 am

Very nicely covered and it works too.  Many thanks.  Let me if you any such more tutorials.

n/a Points
2014-03-09 1:39 am

super tips

n/a Points
2014-03-09 3:07 am

Hello Sir/mam,

Can you pls provide me with the code for creating discussion forum.

Thank you

Staff
7,372 Points
2014-03-10 10:51 am
Hello Abhishek,

Creating a discussion forum is much more complex. My recommendation would be to use something like PHPBB to do so.

Hello Sam A,

To use a custom alignment, you would need to use some custom CSS to show them appropriately.
n/a Points
2014-03-10 9:16 am

Hi ,

I am trying put the comment box is right of the form. after the name and email feilds..

Please help me..

Thanks.

 

n/a Points
2014-04-07 5:05 am

hello sir/mam

can you pls provide me with the code for posting comment which are add below.

Thank you

Staff
15,308 Points
2014-04-07 10:38 am
Hello bhaskar,

We currently do not have general code samples for creating a comment section. This type of feature is usually included as part of the Content Management System such as WordPress, Joomla, Drupal, etc. Creating a comment feature would take much more coding and database interaction. You may want to check and see if there is such a feature for your program. Also, if it uses modules and plugins, there may be one for comments.

Kindest Regards,
Scott M
n/a Points
2014-04-09 10:22 am

Thanks for the Help !!!!!!!!!!!!!!!!!! :)

n/a Points
2014-04-16 7:20 am

hello sir/mam

 how can sent a comment/text one page to another page without using sesssion is any diffrent way to sent some text one page to another page.

 

Staff
15,484 Points
2014-04-16 12:24 pm
Hello Suman,

Thanks for the question. This kind of thing can be done many different ways depending on the code that you're using. We can't always provide you the specific code, but hopefully, this link provides a PHP solution that meets your needs. You can find a tutorial on passing post data here.

Regards,
Arnel C.
n/a Points
2014-05-13 8:16 am

I have followed your tutorials deligently and implemented all the steps, but got stock when comments from my example pages where not recorded at the database. every other thing works well. How do i get comment register at the database?

Staff
7,266 Points
2014-05-13 11:16 am
Hello charlo,

Thank you for your question. Did you complete Sections 2-6 in our series on Using PHP to create dynamic pages?

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-07-25 5:42 am

Below is the php code that give the error message: Notice: Undefined index: comment in C:\xampp\htdocs\testnew\manage_comments.php on line 16

Please help.

<?php

if( $_POST )

{

$con = mysql_connect("localhost", "root", "magicfouru");

 

if(!$con)

{

die('Could not connect:' . mysql_error());

}

 

mysql_select_db("testnew", $con);

 

$users_name = $_POST['name'];

$users_email = $_POST['email'];

$users_website = $_POST['website'];

$users_comment = $_POST['comment'];

 

$users_name = mysql_real_escape_string($users_name);

$users_email = mysql_real_escape_string($users_email);

$users_website = mysql_real_escape_string($users_website);

$users_comment = mysql_real_escape_string($users_comment);

 

$articleid = $_GET['id'];

if( ! is_numeric($articleid) )

die('invalid article id');

 

$query = "INSERT INTO `testnew`.`comments` (`id`, `name`, `email`, 

`website`, `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name', 

'$users_email', '$users_website', '$users_comment', 

CURRENT_TIMESTAMP, '$articleid');";

 

 

mysql_query($query);

 

echo "<h2>Thank you for your comment!</h2>";

 

mysql_close($con);

}

?>

Staff
7,266 Points
2014-07-25 10:40 am
Hello Charlo,

Thank you for providing detailed information. This may be caused by a user leaving nothing in the comment field.

Try using something like this, which will just fill in the field:

if ($_POST['comment'] != "")
$users_comment = $_POST['comment'];
else
$users_comment = "Your comment was blank!";


I hope this helps. If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-07-27 2:56 am

Thank you for your response.

I have used the code provided and have this parse error: syntax error, unexpected ')' in C/xampp/htdocs/testnew/magae_comments.php on line 18. Is there something I did not do well? Please help me. Thank you.

Staff
15,308 Points
2014-07-27 6:29 pm
Hello Charlo,

Without seeing your exact code, I cannot tell you exactly what to change. However, check all your parentheses to ensure they are in pairs, for ever ( you must have a ). If you have an odd number, then you will experience an error like this.

Kindest Regards,
Scott M
n/a Points
2014-05-30 3:29 am

hello sir can u help me to validate a login section like username and password validation using javascript function in a html page

Staff
9,521 Points
2014-05-30 6:04 am
Hello Sunny, and thanks for your comment.

What did you want to validate your username and password sections against, just that they were filled in? You might have better luck with simply using HTML5 form validation that is now available. For instance you could just use the required option like this:

<input type="text" name="username" required>


For password validation, you might want to try something using a combination of both like this password validation using regular expressions and HTML5.

Please let us know if you had any other questions.

- Jacob
n/a Points
2014-06-25 7:13 am

nice man..good work..i like this article

n/a Points
2014-07-09 11:40 am
Include videos and some other interactive media.
Staff
9,521 Points
2014-07-14 3:34 pm
Hello Gerard, and thank you for your suggestion.

I'll go ahead and add this article to our list of ones that could use an update with some video.

Thanks again.

- Jacob
n/a Points
2014-07-21 2:20 am

hi, 

  please sned me the following code Abt PHp 

Staff
7,266 Points
2014-07-21 10:19 am
Hello Bala,

Thank you for contacting us today. We are happy to help, but I am not sure what you asking for.

Please provide more detailed information, so we can assist you further.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-07-23 2:09 am

hello,

    i need to create one Contact us Page using PHP please sendme the sample code 

Staff
5,391 Points
2014-07-23 7:25 am
Hi Bala,

If you need sample code for creating a contact page, I recommend reading the tutorial series we have here: 205: Using PHP to create dynamic pages

You may also find it easier watching a video on how to use PHPMailer or this article about using FormMail.
n/a Points
2014-07-24 12:46 am

thank u verymuch ,its very use full thank u so much  and nice to meet u 

n/a Points
2014-07-21 3:24 am

helo please help me?

i can't manage to connect my website to mysq. i install wamp software and created a database that i want to connect to my website but i can't able to connect it help me

Staff
15,308 Points
2014-07-21 9:20 am
Hello Eskinder,

To connect your php to a myswl database, you will want to take a look at our article on Using PHP to insert Mysql

Kindest Regards,
Scott M
n/a Points
2014-07-24 9:40 am

Hello sir,

  i want a code that when i click a submit button it should be submitted and i should get a another comment box to post oyhers also with reply like your comment box.

Staff
7,372 Points
2014-07-24 10:35 am
What you are describing would need a significant amount of code within both the comment box, as well as the way the page is displayed in which we unfortunately cannot develop this entire solution for you.
One known commenting system is Disqus which will handle all of your commenting code for you. Outside of this, you may want to speak to a developer to have it custom coded for you.
n/a Points
2014-07-24 2:13 pm

Thank so much for your kind reply. I did followed the lessons 2-6 through to the final stage , but yet comment from the pages does not register in database and does not show on the pages. Instead I got this Notice: Undefined index: comment in C:/xampp/htdocs/testnew/manage_comments.php on line 16 How do I get this resolve? Please help.

Staff
7,372 Points
2014-07-24 2:21 pm
To identify the issue, could you provide us with the code that you have on line 16 as well as the surrounding lines?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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

33 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

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: support@InMotionHosting.com
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!