+ Reply to Thread
Results 1 to 4 of 4

Thread: GET and POST

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    GET and POST

    In this tutorial, I will show you a simple example of using POST or GET in a form on a page to simply print “Hello, NAMEHERE”. Of course, POST and GET can be used for better things than this, like sending form information, but this will be enough so that I can show you the difference between them.

    So, firstly, we have our form

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <
    html>
    <
    head>
      <
    meta content="text/html; charset=ISO-8859-1"
     
    http-equiv="content-type">
      <
    title></title>
    </
    head>
    <
    body>
    <
    form method="post" name="posttest">Name: <input
     name
    ="namevar">
    <
    input name="Submit" value="Submit" type="submit">
    </
    form>
    </
    body>
    </
    html
    Notice in the form tag how we have set method to “post”, this means that the form will submit all values with the post method (so in this case, it will post a variable called namevar). In PHP we can retrieve variables from the post method by using $_POST[‘variablename’]. Since we are using a page that is posting to itself, we will test to see weather the variable exists before we echo anything.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <?php if ($_POST['namevar'] != "")
    {
    echo 
    'Hello, '$_POST['namevar'];
    }
    ?>
    <form method="post" name="posttest">Name: <input
     name="namevar">
    <input name="Submit" value="Submit" type="submit">
    </form>
    </body>
    </html>
    Take notice of how when the page refreshes, the URL doesn’t change.

    Now we will use the GET method. First we need to change our form to use the GET method:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <
    html>
    <
    head>
      <
    meta content="text/html; charset=ISO-8859-1"
     
    http-equiv="content-type">
      <
    title></title>
    </
    head>
    <
    body>
    <
    form method="get" name="gettest">Name: <input
     name
    ="namevar">
    <
    input name="Submit" value="Submit" type="submit">
    </
    form>
    </
    body>
    </
    html
    In PHP, we can get GET variables by using $_GET[‘variablename’] and again, before printing this variable out, we will test to see if its there.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1"
     http-equiv="content-type">
      <title></title>
    </head>
    <body>
    <?php if ($_GET['namevar'] != "")
    {
    echo 
    'Hello, '$_GET['namevar'];
    }
    ?>
    <form method="get" name="gettest">Name: <input
     name="namevar">
    <input name="Submit" value="Submit" type="submit">
    </form>
    </body>
    </html>
    However, notice this time that it when the page refreshes it has appended the variable onto the end of the url in the manner of ?namevar=NAMEHERE

    GET variables are restricted in length by URL length restrictions. Generally you would use GET to, you guessed it, GET information. Take a forum for example, most forums will use GET variables to show you a topic. URLs with GET variables can be bookmarked, so this is a good idea. However, when you post a message, it uses post variables. For more information on GET vs POST, you can google it, but be careful, sometimes the explanations you find can be confusing.

    If this tutorial has been helpful, don’t forget to +rep me!
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: GET and POST

    Not bad, you forgot to mention $_REQUEST. +rep

  4. #3
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: GET and POST

    Simple and straight forward, thanks +rep

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: GET and POST

    Good one +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts