using POST from one page to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    using POST from one page to another

    Hi All,

    This is a very basic thing but I can't get it quite right. I have 2 pages. The first page is a login page. The second page checks the password and username and redirects back to the first page if they are not correct.

    On the second page, I have a POST variable that contains an error message. When I am redirected back to the first page, the variable is nowhere to be found. I can achieve what I need with a session but the sessions don't update immediatley and besides, I don't think it is right.

    What is the trick to using POST to do this?

    Thanks,

    Frank
  • Nert
    New Member
    • Nov 2006
    • 64

    #2
    Originally posted by fjm
    Hi All,

    This is a very basic thing but I can't get it quite right. I have 2 pages. The first page is a login page. The second page checks the password and username and redirects back to the first page if they are not correct.

    On the second page, I have a POST variable that contains an error message. When I am redirected back to the first page, the variable is nowhere to be found. I can achieve what I need with a session but the sessions don't update immediatley and besides, I don't think it is right.

    What is the trick to using POST to do this?

    Thanks,

    Frank
    Hi fjm,

    Can we see your code so we find solutions?

    Or maybe this function will help you out..,
    using the extract() function.
    example from a post method.
    save this to example.php
    [HTML]
    <form name="form" method="post" action="login.p hp">
    <input name="uname" type="text" value="my username"/>
    <input name="pword" type="password" value="my password" />
    <input name="submit" type="submit" value="login"/>
    </form>
    [/HTML]

    and the login.php that will extract the content of the post method.
    save this as login.php
    [PHP]
    extract($_POST) ;
    if(isset($uname ) && isset($pword)){
    echo "Your username is ".$uname."< br />";
    echo "Your password is ".$pword;
    }
    [/PHP]

    It'll automatically creates variables that are named according to the name of the textfields, buttons or any objects that are inside the form and send using the post method.

    Comment

    • fjm
      Contributor
      • May 2007
      • 348

      #3
      Originally posted by Nert
      Hi fjm,

      Can we see your code so we find solutions?

      Or maybe this function will help you out..,
      using the extract() function.
      example from a post method.
      save this to example.php
      [HTML]
      <form name="form" method="post" action="login.p hp">
      <input name="uname" type="text" value="my username"/>
      <input name="pword" type="password" value="my password" />
      <input name="submit" type="submit" value="login"/>
      </form>
      [/HTML]

      and the login.php that will extract the content of the post method.
      save this as login.php
      [PHP]
      extract($_POST) ;
      if(isset($uname ) && isset($pword)){
      echo "Your username is ".$uname."< br />";
      echo "Your password is ".$pword;
      }
      [/PHP]

      It'll automatically creates variables that are named according to the name of the textfields, buttons or any objects that are inside the form and send using the post method.
      Hi Nert,

      Thanks for the effort and the response. Unfortunately, your suggestion didn't work.

      I don't have the code here or I would be happy to post it. This is truly as simple as passing a variable from one page to another. I'm not sure if the variable is somehow getting lost when the header() redirects or what..

      If anyone else has anything, I sure would appriciate it.

      Thanks,

      Frank

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        I think you are misunderstandin g how POST variables work.

        POST variables are mainly used to post data from HTML forms to server-side scripts like PHP. They will not exist in a page unless you send the along with the HTTP request from the client browser.

        What I think you are trying to do, and please correct me if I am wrong, is store a variable to be used on multiple pages. If so you will need to use sessions.

        Check out these articles, they may explain where and how to use POST and sessions.
        - Using HTML forms to pass data to PHP
        - PHP Sessions

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #5
          Originally posted by Atli
          I think you are misunderstandin g how POST variables work.

          POST variables are mainly used to post data from HTML forms to server-side scripts like PHP. They will not exist in a page unless you send the along with the HTTP request from the client browser.

          What I think you are trying to do, and please correct me if I am wrong, is store a variable to be used on multiple pages. If so you will need to use sessions.

          Check out these articles, they may explain where and how to use POST and sessions.
          - Using HTML forms to pass data to PHP
          - PHP Sessions
          Hi Atli and thank you for the reply.

          You *are* correctly understanding what I want to do woth POST. I thought it could be done once I set the variable as so:

          [PHP]
          <?php
          $myVar = "myVar";

          if(isset($myVar )){
          $_POST['myVar'] = "My Message";
          ?>
          [/PHP]

          So if I am understanding you correctly, you are saying that all variables must come from html input tags?

          Thanks again.

          Frank

          EDIT:
          By the way, thank you for the link and explanation on GET and POST. Very well done and easy to read.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by fjm
            Hi Atli and thank you for the reply.

            You *are* correctly understanding what I want to do woth POST. I thought it could be done once I set the variable as so:

            [PHP]
            <?php
            $myVar = "myVar";

            if(isset($myVar )){
            $_POST['myVar'] = "My Message";
            ?>
            [/PHP]

            So if I am understanding you correctly, you are saying that all variables must come from html input tags?

            Thanks again.

            Frank
            All POST variables usually come from HTML forms, yes.
            The $_POST array in PHP can only be used to read data that was sent from the client to the server. It can not be used to send data to the client or to another PHP page, and all data in the $_POST array will be lost once the server has finished executing the PHP script and sent the response to the client.

            But what you are suggesting can easily be done with sessions.

            Originally posted by fjm
            EDIT:
            By the way, thank you for the link and explanation on GET and POST. Very well done and easy to read.
            Thanks, glad it helped.

            Comment

            • fjm
              Contributor
              • May 2007
              • 348

              #7
              Originally posted by Atli
              All POST variables usually come from HTML forms, yes.
              The $_POST array in PHP can only be used to read data that was sent from the client to the server. It can not be used to send data to the client or to another PHP page, and all data in the $_POST array will be lost once the server has finished executing the PHP script and sent the response to the client.

              But what you are suggesting can easily be done with sessions.


              Thanks, glad it helped.
              Hey Atli,

              Thanks again, I have just decided to use sessions for that. As far as my comment about sessions being *slow*. It was a programming error in a conditional statement that was making it appear as though the session wasn't working properly. Typical *user error*, not php's fault at all.

              Thanks for your responses and help!

              Frank

              Comment

              Working...