Post by Simie on Sept 30, 2006 6:50:07 GMT -5
PHP $_POST and $_GET Methods
Hey there! This is my second tutorial, so as with the first, don't go angry at me if I get something wrong.
Ever wondered how to get form data in php? Well, here I am to tell you. Lets start with $_POST.
$_POST[]
Posting stuff is done by using html forms, which then submit data to a php or other coding language. Heres an example HTML form:
I hope you understand that, or you need to learn HTML. Anyway, you see how all the form elements have name's? That is a must. If you don't have them, PHP can't grab the values.
Here's an example PHP Script to process the form:
What a nice code, now to break it down you ya:
This is the stuff you shouldn't have seen before. To get form data from a posted form you must use "$_POST['FORMname'];". form name would be the name of the form element you added earlier (I told you it was important). Simple eh? Onto the $_GET method.
$_GET[]
This is what google uses. Lets take this simple html form:
you see its almost exactly the same as the first one, except the form action is GET. If you submited the form with the info
username: Simie
password: password
The url would be like this:
"/php.php?username=Simie&password=password"
This method is good for bookmarking, as you can bookmark all the info in the URL. But, someone could read your password in the URL! So don't use it for login or register forms.
The PHP code is exactly the same as the post one, except that the $_POST is now $_GET. Simple eh?
I hope this tought you something, have fun with forms!
Hey there! This is my second tutorial, so as with the first, don't go angry at me if I get something wrong.
Ever wondered how to get form data in php? Well, here I am to tell you. Lets start with $_POST.
$_POST[]
Posting stuff is done by using html forms, which then submit data to a php or other coding language. Heres an example HTML form:
<form action="php.php" method="POST" target="_self" enctype="application/x-www-form-urlencoded">
<input type="text" value="Username" size="10" maxlength="25" name="username" />
<input type="password" size="10" maxlength="25" name="password" />
<input type="submit" value="Submit"/>
</form>
I hope you understand that, or you need to learn HTML. Anyway, you see how all the form elements have name's? That is a must. If you don't have them, PHP can't grab the values.
Here's an example PHP Script to process the form:
<?php
$username = $_POST['username']; // Grab the username
$password = $_POST['password']; // Grab the password
print 'Your username is: '.$username; // Print their username
print '<br />'; // New line
print 'Your password is: '.$password; // Print their password
?>
What a nice code, now to break it down you ya:
$username = $_POST['username'];
$password = $_POST['password'];
This is the stuff you shouldn't have seen before. To get form data from a posted form you must use "$_POST['FORMname'];". form name would be the name of the form element you added earlier (I told you it was important). Simple eh? Onto the $_GET method.
$_GET[]
This is what google uses. Lets take this simple html form:
<form action="php.php" method="GET" target="_self" enctype="application/x-www-form-urlencoded">
<input type="text" value="Username" size="10" maxlength="25" name="username" />
<input type="password" size="10" maxlength="25" name="password" />
<input type="submit" value="Submit"/>
</form>
you see its almost exactly the same as the first one, except the form action is GET. If you submited the form with the info
username: Simie
password: password
The url would be like this:
"/php.php?username=Simie&password=password"
This method is good for bookmarking, as you can bookmark all the info in the URL. But, someone could read your password in the URL! So don't use it for login or register forms.
The PHP code is exactly the same as the post one, except that the $_POST is now $_GET. Simple eh?
I hope this tought you something, have fun with forms!