$_POST not always set?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian Parker

    $_POST not always set?

    Is it possible to click on a button, and not have it's value and name stored
    in _POST?

    I have this script with a button on it. When you click the button, the page
    posts to itself. The first time you click this Save Changes button, _POST
    is empty. After clicking the button, and the page is loaded again, if you
    click Save Changes once more the _POST is properly set. What's up with
    that?



    $studentCode; // Will hold the current student code, if applicable

    ....

    foreach(array_k eys($_POST) as $key)
    {
    $$key = $_POST[$key];
    print "$key is ${$key}<br />";
    echo "It is: " . $_POST["Submit"] . "<br>";
    }


    if ($_POST["Submit"] == "Add Student") // Save button was hit
    addRecord();
    elseif ($_POST["Submit"] == "Save Changes") // Save changes button was hit
    updateStudent() ;
    echo "We're saving changes<br>";
    elseif ($_GET["action"] != "add")
    getOldData($rec ord);



    echo "<table summary=\"\" cellpadding=\"5 \">";

    function printLabelCell( )
    {
    echo "\n<tr><td align=\"right\" >";
    }

    if (isset($_GET["studentCod e"]))
    $studentCode = $_GET["studentCod e"];
    elseif (isset($_POST["studentCod e"]))
    $studentCode = $_POST["studentCod e"];

    if ($studentCode != "")
    $postType = "post?studentCo de=" . $studentCode;
    else
    $postType = "post";

    echo '<form action="addEdit Student.php" method="' . $postType . '">';

    // ... print controls in the cells ...

    echo "<tr><td colspan=\"2\" align=\"center\ ">";

    echo '<br>';

    if ($_GET["action"] == "add" or isset($_POST["firstName"]))
    echo '<input type="Submit" name="Submit" value="Add Student">&nbsp;
    &nbsp; &nbsp; ';
    else
    echo '<input type="Submit" name="Submit" value="Save Changes">';

    echo '&nbsp; &nbsp; &nbsp; <input type="Reset" name="Reset" value="Clear
    Data"><br><br>' ;

    echo "</td></tr></table>";

    echo "</form>";

    function DisplayTextbox( $vname, $size, $value)
    {
    echo "</td><td><Input type=\"text\" name = \"" . $vname . "\" Size = \""
    .. $size . "\" value=\"" . $value . "\"></td></tr>";
    }

    mysql_close($li nk);


    ....



    <Ade
    --
    Adrian Parker. Ordained priest. <adrian.parker@ sympatico.ca>

    "A society that views graphic violence as entertainment ...should not be
    surprised when senseless violence shatters the dreams of it's youngest and
    brightest..." - Ensign (March 2004)


  • Adrian Parker

    #2
    Re: $_POST not always set?


    "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote in message
    news:ZHckc.4185 0$OU.975473@new s20.bellglobal. com...[color=blue]
    > Is it possible to click on a button, and not have it's value and name[/color]
    stored[color=blue]
    > in _POST?
    >
    > I have this script with a button on it. When you click the button, the[/color]
    page[color=blue]
    > posts to itself. The first time you click this Save Changes button, _POST
    > is empty. After clicking the button, and the page is loaded again, if you
    > click Save Changes once more the _POST is properly set. What's up with
    > that?[/color]

    Methinks my problem comes about when I try to force data into _POST.

    method="post?ac tion=....

    Is there a solution to this?


    Adrian


    [color=blue]
    >
    >
    >
    > $studentCode; // Will hold the current student code, if applicable
    >
    > ...
    >
    > foreach(array_k eys($_POST) as $key)
    > {
    > $$key = $_POST[$key];
    > print "$key is ${$key}<br />";
    > echo "It is: " . $_POST["Submit"] . "<br>";
    > }
    >
    >
    > if ($_POST["Submit"] == "Add Student") // Save button was hit
    > addRecord();
    > elseif ($_POST["Submit"] == "Save Changes") // Save changes button was[/color]
    hit[color=blue]
    > updateStudent() ;
    > echo "We're saving changes<br>";
    > elseif ($_GET["action"] != "add")
    > getOldData($rec ord);
    >
    >
    >
    > echo "<table summary=\"\" cellpadding=\"5 \">";
    >
    > function printLabelCell( )
    > {
    > echo "\n<tr><td align=\"right\" >";
    > }
    >
    > if (isset($_GET["studentCod e"]))
    > $studentCode = $_GET["studentCod e"];
    > elseif (isset($_POST["studentCod e"]))
    > $studentCode = $_POST["studentCod e"];
    >
    > if ($studentCode != "")
    > $postType = "post?studentCo de=" . $studentCode;
    > else
    > $postType = "post";
    >
    > echo '<form action="addEdit Student.php" method="' . $postType . '">';
    >
    > // ... print controls in the cells ...
    >
    > echo "<tr><td colspan=\"2\" align=\"center\ ">";
    >
    > echo '<br>';
    >
    > if ($_GET["action"] == "add" or isset($_POST["firstName"]))
    > echo '<input type="Submit" name="Submit" value="Add Student">&nbsp;
    > &nbsp; &nbsp; ';
    > else
    > echo '<input type="Submit" name="Submit" value="Save Changes">';
    >
    > echo '&nbsp; &nbsp; &nbsp; <input type="Reset" name="Reset" value="Clear
    > Data"><br><br>' ;
    >
    > echo "</td></tr></table>";
    >
    > echo "</form>";
    >
    > function DisplayTextbox( $vname, $size, $value)
    > {
    > echo "</td><td><Input type=\"text\" name = \"" . $vname . "\" Size =[/color]
    \""[color=blue]
    > . $size . "\" value=\"" . $value . "\"></td></tr>";
    > }
    >
    > mysql_close($li nk);
    >
    >
    > ...
    >
    >
    >
    > <Ade
    > --
    > Adrian Parker. Ordained priest. <adrian.parker@ sympatico.ca>
    >
    > "A society that views graphic violence as entertainment ...should not be
    > surprised when senseless violence shatters the dreams of it's youngest and
    > brightest..." - Ensign (March 2004)
    >
    >[/color]


    Comment

    • Jan Pieter Kunst

      #3
      Re: $_POST not always set?

      In article <ZHckc.41850$OU .975473@news20. bellglobal.com> ,
      "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
      [color=blue]
      > if ($studentCode != "")
      > $postType = "post?studentCo de=" . $studentCode;
      > else
      > $postType = "post";
      >
      > echo '<form action="addEdit Student.php" method="' . $postType . '">';[/color]


      As far as I know, this isn't correct. method is either "post" or "get".
      If the method is "post", you put your variables into $_POST like this:

      echo '<form action="addEdit Student.php" method="post">' ;
      echo '<input type="hidden" name="studentCo de" value="' . $studentCode .
      '">';
      echo '</form>';

      JP

      --
      Sorry, <devnull@cauce. org> is een "spam trap".
      E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

      Comment

      • Pedro Graca

        #4
        Re: $_POST not always set?

        Jan Pieter Kunst wrote:[color=blue]
        > In article <ZHckc.41850$OU .975473@news20. bellglobal.com> ,
        > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
        >[color=green]
        >> if ($studentCode != "")
        >> $postType = "post?studentCo de=" . $studentCode;
        >> else
        >> $postType = "post";
        >>
        >> echo '<form action="addEdit Student.php" method="' . $postType . '">';[/color]
        >
        >
        > As far as I know, this isn't correct. method is either "post" or "get".
        > If the method is "post", you put your variables into $_POST like this:
        >
        > echo '<form action="addEdit Student.php" method="post">' ;
        > echo '<input type="hidden" name="studentCo de" value="' . $studentCode .
        > '">';
        > echo '</form>';[/color]

        You can do a POST and pass parameters in the URL, then treat them as if
        they were a GET:

        <form action="whateve r.php?course=17 " method="post">
        <input type="hidden" name="studentCo de" value="542"/>
        <input type="submit"/>
        </form>

        The script that receives the POST from this form can do

        ## course is available in the $_GET array,
        ## but this was NOT a GET request!
        if ($_GET['course'] != '17') {
        exit('Error: course must be 17.');
        }

        if ($_POST['studentCode'] != '542') {
        exit('Error: studentCode must be 542.');
        }


        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        • Kelly Thompson

          #5
          Re: $_POST not always set?

          On 29 Apr 2004 20:46:56 GMT
          Pedro Graca <hexkid@hotpop. com> wrote:
          [color=blue]
          > Jan Pieter Kunst wrote:[color=green]
          > > In article <ZHckc.41850$OU .975473@news20. bellglobal.com> ,
          > > "Adrian Parker" <adrian.parker@ NOSPAMsympatico .ca> wrote:
          > >[color=darkred]
          > >> if ($studentCode != "")
          > >> $postType = "post?studentCo de=" . $studentCode;
          > >> else
          > >> $postType = "post";
          > >>
          > >> echo '<form action="addEdit Student.php" method="' . $postType[/color]
          > >. '">';
          > >
          > >
          > > As far as I know, this isn't correct. method is either "post" or
          > > "get". If the method is "post", you put your variables into $_POST
          > > like this:
          > >
          > > echo '<form action="addEdit Student.php" method="post">' ;
          > > echo '<input type="hidden" name="studentCo de" value="' .
          > > $studentCode . '">';
          > > echo '</form>';[/color]
          >
          > You can do a POST and pass parameters in the URL, then treat them as
          > if they were a GET:
          >
          > <form action="whateve r.php?course=17 " method="post">
          > <input type="hidden" name="studentCo de" value="542"/>
          > <input type="submit"/>
          > </form>
          >
          > The script that receives the POST from this form can do
          >
          > ## course is available in the $_GET array,
          > ## but this was NOT a GET request!
          > if ($_GET['course'] != '17') {
          > exit('Error: course must be 17.');
          > }
          >
          > if ($_POST['studentCode'] != '542') {
          > exit('Error: studentCode must be 542.');
          > }[/color]

          You can also do

          $_FOO = array_merge($_G ET, $_POST);

          and use $_FOO instead of $_GET or $_POST. Just remember that the keys
          from $_POST will overwrite any equal key on $_GET.

          Comment

          • Pedro Graca

            #6
            Re: $_POST not always set?

            Kelly Thompson wrote:[color=blue]
            > You can also do
            >
            > $_FOO = array_merge($_G ET, $_POST);
            >
            > and use $_FOO instead of $_GET or $_POST. Just remember that the keys
            > from $_POST will overwrite any equal key on $_GET.[/color]

            Or use the $_REQUEST array, which inludes $_GET, $_POST, and $_COOKIE :)

            --------

            <?php // xx.php
            setcookie('all' , 'cookie');
            setcookie('COOK IE', 'cookie');
            ?>

            <form action="xx2.php ?all=get&GET=ge t" method="post">
            <input type="hidden" name="all" value="post"/>
            <input type="hidden" name="POST" value="post"/>
            <input type="submit"/>
            </form>

            --------

            <?php // xx2.php
            header('Content-Type: text/plain');

            echo '$_GET: '; print_r($_GET);
            echo '$_POST: '; print_r($_POST) ;
            echo '$_COOKIE: '; print_r($_COOKI E);
            echo '$_REQUEST: '; print_r($_REQUE ST);
            ?>

            --------

            Result is:

            $_GET: Array
            (
            [all] => get
            [GET] => get
            )
            $_POST: Array
            (
            [all] => post
            [POST] => post
            )
            $_COOKIE: Array
            (
            [COOKIE] => cookie
            [all] => cookie
            )
            $_REQUEST: Array
            (
            [all] => cookie
            [GET] => get
            [POST] => post
            [COOKIE] => cookie
            )

            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            • Jedi121

              #7
              Re: $_POST not always set?

              Adrian Parker a écrit le 29/04/2004 :
              [...][color=blue]
              > if ($studentCode != "")
              > $postType = "post?studentCo de=" . $studentCode;
              > else
              > $postType = "post";
              >
              > echo '<form action="addEdit Student.php" method="' . $postType . '">';
              >[/color]

              Not checked but consider puting the studentCode= in the target URL of
              the form and not in the method.
              Try this :

              if ($studentCode != "") {
              $postUrl = "addEditStudent .php?studentCod e=".$studentCod e;
              } else {
              $postUrl = "addEditStudent .php";
              }
              echo '<form action="'.$post Url.'" method="POST">' ;


              Comment

              • Eric Bohlman

                #8
                Re: $_POST not always set?

                Pedro Graca <hexkid@hotpop. com> wrote in news:c6rtun$fkl fl$1@ID-
                203069.news.uni-berlin.de:
                [color=blue]
                > Or use the $_REQUEST array, which inludes $_GET, $_POST, and $_COOKIE :)[/color]

                That depends on a setting in php.ini, so I wouldn't recommend it to newbies
                or to anyone who doesn't have complete control over their server. Using it
                mindlessly can result in code that mysteriously breaks when moved to
                another system.

                Comment

                • Kelly Thompson

                  #9
                  Re: $_POST not always set?

                  On 29 Apr 2004 23:05:36 GMT
                  Eric Bohlman <ebohlman@earth link.net> wrote:
                  [color=blue]
                  > Pedro Graca <hexkid@hotpop. com> wrote in news:c6rtun$fkl fl$1@ID-
                  > 203069.news.uni-berlin.de:
                  >[color=green]
                  > > Or use the $_REQUEST array, which inludes $_GET, $_POST, and
                  > > $_COOKIE :)[/color]
                  >
                  > That depends on a setting in php.ini, so I wouldn't recommend it to
                  > newbies or to anyone who doesn't have complete control over their
                  > server. Using it mindlessly can result in code that mysteriously
                  > breaks when moved to another system.[/color]

                  Which setting?

                  Comment

                  • Pedro Graca

                    #10
                    Re: $_POST not always set?

                    Kelly Thompson wrote:[color=blue]
                    > On 29 Apr 2004 23:05:36 GMT
                    > Eric Bohlman <ebohlman@earth link.net> wrote:
                    >[color=green]
                    >> Pedro Graca <hexkid@hotpop. com> wrote in news:c6rtun$fkl fl$1@ID-
                    >> 203069.news.uni-berlin.de:
                    >>[color=darkred]
                    >> > Or use the $_REQUEST array, which inludes $_GET, $_POST, and
                    >> > $_COOKIE :)[/color]
                    >>
                    >> That depends on a setting in php.ini, so I wouldn't recommend it to
                    >> newbies or to anyone who doesn't have complete control over their
                    >> server. Using it mindlessly can result in code that mysteriously
                    >> breaks when moved to another system.[/color][/color]

                    Thank you for pointing that out, Eric.
                    [color=blue]
                    > Which setting?[/color]

                    gpc_order


                    In my php.ini it is

                    gpc_order = "GPC"

                    so if there is a $_GET['x'], a $_POST['x'], and a $_CCOKIE['x']
                    on my configuration $_REQUEST['x'] will be $_COOKIE['x']

                    --
                    USENET would be a better place if everybody read: : mail address :
                    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                    http://www.expita.com/nomime.html : to 10K bytes :

                    Comment

                    • Kelly Thompson

                      #11
                      Re: $_POST not always set?

                      On 30 Apr 2004 00:27:22 GMT
                      Pedro Graca <hexkid@hotpop. com> wrote:
                      [color=blue]
                      > Kelly Thompson wrote:[color=green]
                      > > On 29 Apr 2004 23:05:36 GMT
                      > > Eric Bohlman <ebohlman@earth link.net> wrote:
                      > >[color=darkred]
                      > >> Pedro Graca <hexkid@hotpop. com> wrote in news:c6rtun$fkl fl$1@ID-
                      > >> 203069.news.uni-berlin.de:
                      > >>
                      > >> > Or use the $_REQUEST array, which inludes $_GET, $_POST, and
                      > >> > $_COOKIE :)
                      > >>
                      > >> That depends on a setting in php.ini, so I wouldn't recommend it[/color]
                      > >to> newbies or to anyone who doesn't have complete control over
                      > >their> server. Using it mindlessly can result in code that
                      > >mysteriously > breaks when moved to another system.[/color]
                      >
                      > Thank you for pointing that out, Eric.
                      >[color=green]
                      > > Which setting?[/color]
                      >
                      > gpc_order
                      >
                      > In my php.ini it is
                      >
                      > gpc_order = "GPC"
                      >
                      > so if there is a $_GET['x'], a $_POST['x'], and a $_CCOKIE['x']
                      > on my configuration $_REQUEST['x'] will be $_COOKIE['x'][/color]

                      Then one more reason to do it yourself with array_merge().

                      Comment

                      • lawrence

                        #12
                        Re: $_POST not always set?

                        I often run into situations where there is nothing stored in $_POST.
                        For me, whenever I run into this, it turns out that the version of PHP
                        on the server I'm on is pre- version 4.1.

                        Comment

                        Working...