Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,326 Categories: 12

Total Tutorial Views: 41,273,480

How to Create a simple PHP form

Tutorial Name: How to Create a simple PHP form  

Category: PC Tutorials

Submitted By: Mitzz

Date Added:

Comments: 1

Views: 3,972

Related Forum: PC Building Forum

Share:

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.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple PHP form</title>
</head>
<body>
<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" />
</form>
</body>
</html>

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:
<?php
if (isset($_GET['name'])) {
$val1=$_GET['value1'];
$val2=$_GET['value2'];

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">

Ratings

Current rating: 3.08 by 12 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"How to Create a simple PHP form" :: Login/Create an Account :: 1 comment

If you would like to post a comment please signin to your account or register for an account.

TransparencyPosted:

http://www.codecademy.com/tracks/php