Tutorials PHP Send Data with GET and POST

Send Data with GET and POST

Today's lesson builds upon last week's lesson on forms. Actually, this lesson should have been first to grab the data before validation. Oh well, no harm no foul. Anyways, there are two ways to send data through PHP: GET and POST. They are relatively simple to use.

What is Post?

POST is data sent through a form. When you create a form, the most inportant attribute on your inputs tag is name. When you submit the form, PHP uses the name attribute to retrieve the data.

<?phpif (isset($_POST['submit'])) {  $data = $_POST['data'];}?><form action="form.php" method="post"><input type="text" name="data" /><input type="submit" name="submit" value="submit"/></form>

What is Get?

GET is data sent through the URL. Whenever you see a weird URL that doesn't make any sense, they are using GET to grab data. This very page is using GET to grab the id of this article from the database. To create GET, you need to put a question mark after the file followed by the name and the actual data.

// The URL is http://mysite.com/page.php?data=123$data = $_GET['data'];// This produces "123"echo $data;

Which is the better option?

Depends on what you're using it for. POST is sent through forms and get is sent through URLs. The general practice is to use POST when you actually need to do something, and use GET when you only need to display something. It is a lot easier to use forms if you need to send multiple pieces of data. Also note that the maximum number of characters in a URL is 255 while there is no limit to data from forms.

That's it for today. Try sending data to yourself and see if you can actually receive it. Also, if you are planning on making a user submitted form, check out this tutorial for form validation. See you all next week!

Posted by on . Category: PHP


No comments posted yet

You need to register or login to post new comments.