Where do i start?
Okay, when setting up a html form, you need to open with the form tag, and then you need to specify how the information is going to be passed over to the page/script that is going to handle the form.
The way we define the method of sending the information, we use the
METHOD form attribute, which can be wither
get or
post.. and to define where the form is going to be aimed at, we use the
ACTION form attribute.
| Code |
<form action="login.php" method="post">
Username: <input type="text" name="UID" /><br />
Password: <input type="password" name="PASSWORD" /><br />
<input type="submit" value="Login >>" />
</form> |
The above results from the form are going to be sent to the page "login.php", using the method "post". "Post" basically means that the variables are going to be hidden from the user, whereas the "get" method would have the variables showing in the URL.
Okay. Thats great. Now what do I do with the information that was entered?
The information that was entered in the form can be accessed in one of two ways. If the method of the form was
"post", then all of the variables are going to be held in the array
$_POST, whereas if the method of the form was
"get", then all of the variables are going to be held in
$_GET. When building a script, if you are unsure of how the variables are going to be coming in, or if you know that you are going to need to accept the variables from both "get" and "post" methods, then you can access both "post" and "get" variables from the
$_REQUEST array.
To access the different elements of the form (e.g. textboxes, textareas), you need to give them all a unique name on the html form. As you can see above, the name of the text element is "UID", and the name of the password box is "PWD". The method is "post", so to get at the contents of the username box, I can type.. $_POST['UID']. Easy? Easy.
Now, to prove that I am not making this up, I will complete the login.php section of this tutorial, and then you can run it on your local servers / websites, to see that it actually does work!
| PHP Code |
<?php
//on the form above, the method was set to "post"
$username = $_POST['UID'];
$password = $_POST['PWD'];
//if the username given is "nick", and the password is "iloveima"
if($username == "nick" && $password" == "iloveima")
{
echo "WELCOME TO YOUR SECRET AREA $username!";
}
else
{
echo "INVALID USERNAME AND PASSWORD COMBINATION!";
}
?>
|
Enjoy =D