Tutorial
Introduction
In this tutorial we are going to learn how we can use user input variables.
For example a login form as shown on the right side of this website.
You would have to somehow get the variables out of the text fields and use them in your script...
This tutorial will tell you how to do that.
( After the basic tutorials we will explain how to combine this with SQL, login forms etc. )
Step 1
We are going to create a form where a user will have to fill in his
username, password( 2 times ), age and his e-mail adress.
Create a new PHP file, name it for example form.php.. just what ever you like.
then put this code in:
Code:
<?php
echo "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' >
<title>User input form</title>
</head>
<body>
<form action='' method='POST'>
<input type='text' name='username'>Username<br>
<input type='password' name='password'>Password<br>
<input type='password' name='repeat'>Confirm<br>
<input type='text' name='age'>Age<br>
<input type='text' name='email'>E-mail<br>
<input type='submit' name='register' value='Register'>
</body>
</html>";
?>
Step 2 Explaining the basics
We now have an HTML form which should look a little something like this:
In the form tag we tell the browser that the action of the form will be in
the same file, and the method in what way the data will be sent is POST.
POST variables are put in a "POST array" once submitted.
( we will explain more about this later on. )
Now in the input tag we use the tag: name.
The value you fill in here.. will be your POST name.
The image below will show you how the values automaticly changes after you have pressed the button.
You can see that automaticly the POST array will get filled with the values you've made.
$_POST is a standard PHP array value.
$_POST['username'] will return the value of that specific part of the array, in this case this will be the username.
Step 3 Using POST values in PHP
We are now going to make the action of the form, so we will need to adjust the file we made earlier:
Code:
<?php
if($_POST['register'])// This is the POST value of the button.
{
if($_POST['age'] >= 13)
{
echo "Dear ".$_POST['username'].",<br>
You were old enough to register.<br>
An e-mail will be sent to: ".$_POST['email'];
}
else
{
echo "I'm sorry you need to be atleast 13 to register.";
}
}
else
{
echo "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' >
<title>User input form</title>
</head>
<body>
<form action='' method='POST'>
<input type='text' name='username'>Username<br>
<input type='password' name='password'>Password<br>
<input type='password' name='repeat'>Confirm<br>
<input type='text' name='age'>Age<br>
<input type='text' name='email'>E-mail<br>
<input type='submit' name='register' value='Register'>
</body>
</html>";
}
?>
In this form you can see we capture the values filled into the form by using the POST array.
By using an if-else statement we can say, if the button is pressed.. check if the age that has been filled in is bigger or equals 13,
If not... then we tell the user he's not allowed to register.
A piece of cake don't you think?
In later tutorials we will create a proper register form with PHP/MYSQL.
I hope this tutorial was clear enough, if there are any questions I'd like to know so i could explain it to you.
You now know how to use forms with PHP.
| « Previous | Next » |
Comments
Flamer - 2008-12-02
Hi there Remco!
sup ?
I once again think that ur tut is fine.
I always use the REQUEST array when using forms, dont have to bother whether its a POST or a GET array where i stored my information.
I once again think that ur tut is fine.
I always use the REQUEST array when using forms, dont have to bother whether its a POST or a GET array where i stored my information.
Remco - 2008-12-09
Yoo flamer, ofcourse there are different ways of doing it
... but I find this an easy way of explaining the different possibilities to people.
The aim of the tutorials are to explain the basic possibilities of PHP.
After my tutorials a person should be able to create their own applications.
A good way of doing this is to do it step by step, and then set goals for yourself. Just try and make a cms for example.. and you will automaticly need functions etc.. you will look for it.. and you will learn
The aim of the tutorials are to explain the basic possibilities of PHP.
After my tutorials a person should be able to create their own applications.
A good way of doing this is to do it step by step, and then set goals for yourself. Just try and make a cms for example.. and you will automaticly need functions etc.. you will look for it.. and you will learn
You have to be logged in to write a comment.



