|
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
|
|
|
|
|
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="" />
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
|
I used ID before and it worked Or at least thats what my memory tells me... (I think its flawed...)
|
|
|
|