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!
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]
<?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.
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.
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).
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.
$_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).