Dreevoo.com | Online Learning and Knowledge Sharing
Home | Programs | Programming languages | PHP & MySQL | Create a simple PHP form
Click to view your profile

Create a simple PHP form

Today we will learn how to create a simple form in HTML and how to use this data in PHP.

  Author: podtalje | Version: php.net | 27th February 2013 |  

If you need help setting up a PHP environment on your computer, please click here.

If you want to learn the first steps of PHP coding, click here.

First we will create a simple HTML form which will contain three input fields:
- Name
- Value 1
- Value 1

We will then handle the data user submitted with PHP:


To create a form you can paste the code bellow into your favorite text editor and save it under name lesson3.php.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple PHP form</title>
<h2>Simple PHP form</h2>
<form method="get" action="lesson3.php">
Name: <input type="text" name="name" /> <br />
Value 1: <input type="text" name="value1" /> <br />
Value 2: <input type="text" name="value2" /> <br />
<input type="submit" value="Submit" />


When the user will enter the data into input fields and press Submit button, the data will be sent to the page, specified by the action parameter in form from previous step.

In our case we have used name lesson3.php which means that the data will be sent to our document.

Now we must add some PHP code to handle the input data.

Below the form we add the following code:
if (isset($_GET['name'])) {
  echo "The user submited form  ".$_GET['name']. ' <br />';
  echo "Values submited are: $val1, $val2";

As you can see the data from the user form is available in the $_GET variable under the name of input field.


If we enter the data into the input fields and press Submit button, you should get a result showing the values you entered.


When we specified a form, we defined method="get"

The result is that input values will be appended to URL.

If you want to change this behavior you should change the method to post:
<form method="post" action="lesson3.php">


The only difference you now have to make is to change variables $_GET into $_POST and your code will work correctly.

As always try the code yourself, make some changes and observe the result. And of course in case of any problems we are always available on forum, so don't be afraid to ask.

  Please login to post a comment
  Click to open user profile  
suneeil, 3rd Apr 2013, 4:02 AM
i''m new in php but i enjoy something here ,so thanks..........
  Click to open user profile  
podtalje, 3rd Apr 2013, 4:56 AM
Welcome to our community, suneeil.

If you have any questions, please don't be afraid to ask. We will do our best to give you the best answers and help you with anything.

  Click to open user profile  
phonety, 1st Aug 2013, 10:14 AM
thanks for the simple and encouraging example you load here.
my question is this "after changing the GET to POST , name and values disappear?? is this value store in the database"
  Click to open user profile  
podtalje, 1st Aug 2013, 10:34 AM
No, this value is not stored in database.

Difference between GET and POST is just in a way the data is sent forward to the next page.

In case of GET, the data from the form is appended to to URL after the character ?
(e.g. lesson3.php?name=your_name&value1=1234&value2=5678 )

In case of POST, the URL stays the same (e.g. lesson3.php) and this data is sent in a special packet that is not visible to the end user. Usually you use POST if you want to send a lot of data.

From the programming side of view, if you change form method from GET to POST, you should also change all the variables in the code from $_GET to $_POST
(e.g. $_GET['name'] should become $_POST['name'])

I hope this answers your question. If you have any other questions, just let me know.

  Click to open user profile  
Travolta, 3rd Dec 2013, 7:16 AM
Hello, can someone help me build a forum from scratch? I know little about PHP and MySQL. thanks
online learning made for people