Lesson 14

GET method

So far, we’ve been sending data to the server via the HTML form and using the POST method:

This way the information that is submitted to the server is not “visible” to the user. This is safer to send data across. Should be used specially for passwords, sensitive and long data.

There’s however a different method that allow us to view the data that is being sent to the server: GET

One of the big differences between both is that with GET the info goes in the url. For example, in this next form:

When you click submit you will see the form data being passed on the url:

Why can this be useful? One of the advantages of using this is the ability to share the URL or to try different parameters without having to use the form, just be changing the URL. If we manually change the url to be http://localhost/lesson14/results.php?name=João&country=Portugal we get this result:

Again, this can be great for generating dynamic urls as we will see below.

MySQL – WHERE clause

So far we have learned how to get data from database tables, but when we do we get the entire table. What if we want a specific row of that table? We can do it by using WHERE clause:

or, by using a GET variable:

This way we get all the rows of the table students that have the id field equal to the value we passed.

A complete sample:

You might be questioning why we do $student[0] – this is because SELECT command returns an array of rows, and we do this to get the first item that comes from the DB.

Here we can see how to generate a list of our students with a link for all of them:

« Lesson 13 Index