Creating a simple HTML Form to get user comments
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
- ... 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:
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:
... 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:
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:
- In the form tag, we set the method to post (vs get).
- 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=1PHPandMySQL.inmotiontesting.com/page2.php?id=2Notice 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.
We value your feedback!
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.
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
2014-03-06 8:38 am
Very nicely covered and it works too. Many thanks. Let me if you any such more tutorials.
2014-03-09 3:07 am
Can you pls provide me with the code for creating discussion forum.
2014-03-10 9:16 am
I am trying put the comment box is right of the form. after the name and email feilds..
Please help me..
2014-03-10 10:51 am
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.
2014-04-07 5:05 am
can you pls provide me with the code for posting comment which are add below.
2014-04-07 10:38 am
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.
2014-04-16 7:20 am
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.
2014-04-16 12:24 pm
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.
Post a Comment
Do you want to publish a tutorial to our support center?