Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Simie

Simie Avatar

******
ProScripter

1,052


May 2006
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:
<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! :)


Last Edit: Oct 1, 2006 9:54:33 GMT by Simie

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Most HTML forms require NAME along with ID for form elements usually. Otherwise the data is not always recieved. ID is used for styling it or label's usually.


<label for="whee">Blah:</label> <input name="whee" id="whee" type="text" value="" />

Simie

Simie Avatar

******
ProScripter

1,052


May 2006
Id works for me :-/ Acctording to W3 the name attribute is depreciated. Meh, I'll change the tutorial :-/


EDIT: Urgh, I misread the stupid XHTML markup thing. >:(


Last Edit: Oct 1, 2006 10:12:15 GMT by Simie

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
simie said:
Id works for me :-/ Acctording to W3 the name attribute is depreciated. Meh, I'll change the tutorial :-/


EDIT: Urgh, I misread the stupid XHTML markup thing. >:(


Name is probably deprecated for usage with anchors (number sign after a URL). MAybe some other things. But its valid for form objects.

Moose

Moose Avatar

****
Senior Member

449


August 2005
Well name is kinda needed for form elements otherwise there would be no way to read your data on the sever side. :P
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Simie

Simie Avatar

******
ProScripter

1,052


May 2006
I used ID before and it worked :-/ Or at least thats what my memory tells me... (I think its flawed...) :P

newBookmarkLockedFalling