Get `n post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
[n00b]
Forum Commoner
Posts: 34
Joined: Sat Mar 20, 2004 7:06 pm

Get `n post

Post by [n00b] »

Hello. First post here :D

See my nick? Then don`t laugh at my questions!!! :P

Ok, here it is:
Whats the difference between GET and POST methods? Any examples?

Say I make a form containing a text field and a submit button which when pressed sends the data in the text field and saves it in a db. Which should I use in this case?[/php_man]
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

hi

you'll see this at

<form action="page.php" method=POST>

generically speaking, POST is when you send variables to a different location. the above link would send the variable(s) in the form to page.php.

GET would look like this:

Code: Select all

<form action=$PHP_SELF method=GET>
this sends the variable(s) to the same page (it refreshed the page with the variables present, basically)
[n00b]
Forum Commoner
Posts: 34
Joined: Sat Mar 20, 2004 7:06 pm

Post by [n00b] »

you mean that post is used to pass variables to another php file while get when a php file is calling itself?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i think u have the idea.

heres an example how one may use the POST method:

Code: Select all

<?php

// this code is on index.php
<form action=verify.php method=POST>
<input type=text name=username>
<input type=submit value=submit>

//then on verify.php

if (isset($username)) {
echo $username;
}

or something like

$name = $_POST["username"];
echo $name;
?>
$_GET would do whats on verifiy.php on index.php (meaning its done on the same page and u need to call the variables sent from the form(thats on index.php) with $_GET)

i'm sorry if i'm confusing you, let me know if i am.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Whenever you see a link like this:

Code: Select all

<a href="foobash.php?Var=3">Link</a>
The variable "Var" is being sent via the "GET" method.
Using a <form method="GET"> would be a bit unusual, as
the usage <form method="POST"> is much more common.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Post is good for most things

Get is good for things where they may want to copy the link, add it to their favorites, send it to a friend, etc (ie. a catalog of items, a specific page on your site, etc).
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Post is always preferred as the form action since it is harder to forge a form than it is to tamper with the query string. Hence, post is slightly more secure.
[n00b] wrote:you mean that post is used to pass variables to another php file while get when a php file is calling itself?
That's not correct: each can be used in either case.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

$_GET is also limited in size. As a rule of thumb it can't be longer than 255 characters. $_POST is not size-restricted and not as transparent (i.e. somewhat more secure).
[n00b]
Forum Commoner
Posts: 34
Joined: Sat Mar 20, 2004 7:06 pm

Post by [n00b] »

guys thanx a lot! You were really helpful! :D

EDIT:
One more question.
It is possible to pass POST vars only using forms?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

unfortunately, yes.

get can be passed with hyperlinks or forms, post vars are only from forms
Post Reply