GET POST or which methode should you use ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bchaib
    New Member
    • Jan 2008
    • 20

    GET POST or which methode should you use ?

    Good time everyone here
    When I created an account on this site I got this url:

    So now I have a question about ?do=register and what does this mean ?
    Suppose you have a web page named main.php with some links to different pages with same layout (one table) but different contents depending on what the visitor clicks.

    For example:
    Link 1 leads you to page with content Hello link 1
    Link 2 ==> Hello link 2
    ...

    Do you have to create 2 pages for this script?

    Let us take a look at the source code of this site:
    Code:
    <form action="http://www.thescripts.com/forum/register.php" > .. </form>
    Another example but not for paging, suppose you wanne make a little quiz system like this following one:
    index.php
    [PHP]<html><head><ti tle>..</title></head>
    <body>
    <form action ="process.ph p" method="post">
    What is democracy? <br />
    <input type="text" id="answer" /><br />
    <input type="submi" id="Control the answer" />
    </form>
    <?php
    $result = flase;
    if ($result) {
    echo("Good answer");
    else
    echo("Try again!");
    }
    ?>
    </body>
    </html>
    [/PHP]
    process.php
    [PHP]<?php
    $answer = $_POST['answer'];
    $result = flase;

    if ($answer = 'democracy means protecting the capitalists')
    $result = true;
    else
    $result = flase;

    Redirect header("Locatio n: index.php");
    ?> [/PHP]

    So how do you link the $result variable with the index.php page or how do you pass this to other pages ?

    Let's make the knowledge/experience open source and share it with each other ;)
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Originally posted by bchaib
    Good time everyone here
    When I created an account on this site I got this url:

    So now I have a question about ?do=register and what does this mean ?
    Suppose you have a web page named main.php with some links to different pages with same layout (one table) but different contents depending on what the visitor clicks.

    For example:
    Link 1 leads you to page with content Hello link 1
    Link 2 ==> Hello link 2
    ...

    Do you have to create 2 pages for this script?

    Let us take a look at the source code of this site:
    Code:
    <form action="http://www.thescripts.com/forum/register.php" > .. </form>
    Another example but not for paging, suppose you wanne make a little quiz system like this following one:
    index.php
    [PHP]<html><head><ti tle>..</title></head>
    <body>
    <form action ="process.ph p" method="post">
    What is democracy? <br />
    <input type="text" id="answer" /><br />
    <input type="submi" id="Control the answer" />
    </form>
    <?php
    $result = flase;
    if ($result) {
    echo("Good answer");
    else
    echo("Try again!");
    }
    ?>
    </body>
    </html>
    [/PHP]
    process.php
    [PHP]<?php
    $answer = $_POST['answer'];
    $result = flase;

    if ($answer = 'democracy means protecting the capitalists')
    $result = true;
    else
    $result = flase;

    Redirect header("Locatio n: index.php");
    ?> [/PHP]

    So how do you link the $result variable with the index.php page or how do you pass this to other pages ?

    Let's make the knowledge/experience open source and share it with each other ;)
    For the example that you have given, you would probably be better off using a session variable if you want to have the value of $result shared among several pages.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      $_GET is normally used in Content Management Systems, i.e. you use $_GET to retrieve which oage should be displayed, then you query your database; which will have a series of stories in it - lets say, then that story will be displayed on the initial page. This method is known as 'dynamic'.

      $_POST, however, is the basis of forms - unlike $_GET which is primarily used on hyperlinks. $_POST has a greater amount of space you can use, compared to $_GET which is limited to a certain amount because the url address bar will only allow so many characters.

      $_POST is also invisible to the world! Therefore, you should use this when passing private information, i.e, passwords, login data, etc.

      Neither one is better to use, they're made for different purposes.

      Comment

      • bchaib
        New Member
        • Jan 2008
        • 20

        #4
        Originally posted by markusn00b
        ...

        Neither one is better to use, they're made for different purposes.
        :-) absolutly, I think I'm going to try sessions ..

        Thanks

        Comment

        • bchaib
          New Member
          • Jan 2008
          • 20

          #5
          Originally posted by stepterr
          For the example that you have given, you would probably be better off using a session variable if you want to have the value of $result shared among several pages.
          Thanks, I'm going to do that .. but is that the only solution?
          You have also things like n-tier application, there you should split your application
          into different layers
          HTML with {example} PHP references
          PHP database
          PHP connection
          PHP variables
          ...

          Just like we do using CSS, Javascript and XHTML.

          I'm looking for a good tutorial using this way ..

          Thank you

          Comment

          • jenkinsloveschicken
            New Member
            • Dec 2006
            • 56

            #6
            Session variables aren't the only solution to the your problem. It really comes down to how you want to handle passing of data in your app. You can $_POST back to the same script using $_server['php_self'] via form, or you can send $_GET variables back to the same script in an href.

            You have to be careful of variable scope if you do this. And you can add some re-usability by filtering the page content using something like:
            Code:
            if (isset($_GET['somevar'] ) {
                   if ($_GET['somevar'] == value1)
                         {
                           echo "something";
                         }else{
                           echo "something else";
                         }
                    }
            to create the window.

            Another method would be to use $_COOKIE to set a cookie using js then read it via embedded function on your page loads that need whatever info that you are needing to be persistent.

            Regards,
            Jenkins

            Comment

            • stepterr
              New Member
              • Nov 2007
              • 157

              #7
              Originally posted by bchaib
              Thanks, I'm going to do that .. but is that the only solution?
              You have also things like n-tier application, there you should split your application
              into different layers
              HTML with {example} PHP references
              PHP database
              PHP connection
              PHP variables
              ...

              Just like we do using CSS, Javascript and XHTML.

              I'm looking for a good tutorial using this way ..

              Thank you
              Like Jenkins says, sessions are not the only solution, cookies are another good option. But if you were to use the $_GET you'd have to link to a page and include the value in the url each time and if you were to use a $_POST then you are would need to have the values in your form and submit it to the other page and then grab it using the $_POST each time. I just find it easier that if I'm going to be using the same value across multiple pages of my site that storing it in a session variable has worked out the best. Now if you are just going to use a value between two pages then the $_GET and the $_POST will be all you need, but it doesn't sound like that was what you were looking for.

              Comment

              • bchaib
                New Member
                • Jan 2008
                • 20

                #8
                But I get some errors :(

                Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at C:\www\www\db\p rocces.php:1) in C:\www\www\db\p rocces.php on line 2

                Warning: Cannot modify header information - headers already sent by (output started at C:\www\www\db\p rocces.php:1) in C:\www\www\db\p rocces.php on line 4

                Comment

                • stepterr
                  New Member
                  • Nov 2007
                  • 157

                  #9
                  Where have you placed the headers on your page?

                  Comment

                  • bchaib
                    New Member
                    • Jan 2008
                    • 20

                    #10
                    Originally posted by stepterr
                    Where have you placed the headers on your page?
                    after the php control:
                    ............... ............... ............... ...........
                    [HTML]<?php
                    session_start() ;
                    if (!IsSet($_SESSI ON['login'])) {
                    header("Locatio n: index.php");
                    exit(0);
                    }

                    ?>

                    <!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dt d">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>

                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                    <title>Title here : some text</title>[/HTML]

                    BUT
                    I have this page saved in a sub directory of data directory.
                    I have the html login control and session variable in another directory!

                    Comment

                    • stepterr
                      New Member
                      • Nov 2007
                      • 157

                      #11
                      Originally posted by bchaib
                      after the php control:
                      ............... ............... ............... ...........
                      [HTML]<?php
                      session_start() ;
                      if (!IsSet($_SESSI ON['login'])) {
                      header("Locatio n: index.php");
                      exit(0);
                      }

                      ?>
                      Take a look at this site about the session_start() function, particularly in the comments below concerning session_start() and header(). http://nl2.php.net/manual/en/function.session-start.php
                      Then you can also look at this link for the header() function. http://nl2.php.net/manual/en/function.session-start.php
                      Both are good to look at to see how you may need to get around this issue.

                      Comment

                      Working...