Foreach element in $_POST?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IWP506@gmail.com

    Foreach element in $_POST?

    Hey

    Is there a way to go through each element in the $_POST supervariable?

    I'm making a page with dynamic forms, so I don't know for sure how many
    $_POST variables there will be (the number of textboxes, radio buttons,
    etc changes).

    How can I either count them or do a foreach loop to do this?

    Thanks

    iwp506@gmail.co m

  • Carl

    #2
    Re: Foreach element in $_POST?

    IWP506@gmail.co m wrote:[color=blue]
    > Hey
    >
    > Is there a way to go through each element in the $_POST supervariable?
    >
    > I'm making a page with dynamic forms, so I don't know for sure how many
    > $_POST variables there will be (the number of textboxes, radio buttons,
    > etc changes).
    >
    > How can I either count them or do a foreach loop to do this?
    >
    > Thanks
    >
    > iwp506@gmail.co m
    >[/color]

    iwp506,

    Easy, just treat $_POST as an array and use foreach.


    Don't forget to make the post values safe before using them if they were
    input by the users...

    Carl.

    Comment

    • IWP506@gmail.com

      #3
      Re: Foreach element in $_POST?


      Carl wrote:[color=blue]
      > IWP506@gmail.co m wrote:[color=green]
      > > Hey
      > >
      > > Is there a way to go through each element in the $_POST supervariable?
      > >
      > > I'm making a page with dynamic forms, so I don't know for sure how many
      > > $_POST variables there will be (the number of textboxes, radio buttons,
      > > etc changes).
      > >
      > > How can I either count them or do a foreach loop to do this?
      > >
      > > Thanks
      > >
      > > iwp506@gmail.co m
      > >[/color]
      >
      > iwp506,
      >
      > Easy, just treat $_POST as an array and use foreach.
      > http://www.php.net/manual/en/control...es.foreach.php
      >
      > Don't forget to make the post values safe before using them if they were
      > input by the users...
      >
      > Carl.[/color]

      Ok, thanks

      Comment

      • Peter Fox

        #4
        Re: Foreach element in $_POST?

        Following on from Carl's message. . .[color=blue]
        >
        >Easy, just treat $_POST as an array and use foreach.
        >http://www.php.net/manual/en/control...es.foreach.php
        >
        >Don't forget to make the post values safe before using them if they were
        >input by the users...
        >[/color]
        Further don't forget that elements of $_POST may be arrays in their own
        right. eg Checkboxes. Here is a snippet of code as illustration


        #---------------------------------------------------------------------
        function POSTget($VarNam e,$tidy=TRUE){
        # read POSTed value from HTTP_POST_VARS
        # Normally we'll tidy up the input to remove leading and trailing spaces
        and control chars
        # but this can be overridden.
        #---------------------------------------------------------------------
        if (isset($_POST[$VarName])){
        $rv = $_POST[$VarName];
        if(!is_array($r v)){
        if ($tidy) {$rv = TidyInput($rv); }
        }
        }else{
        $rv='';
        }
        return $rv;
        }

        --
        PETER FOX Not the same since the porcelain business went down the pan
        peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
        2 Tees Close, Witham, Essex.
        Gravity beer in Essex <http://www.eminent.dem on.co.uk>

        Comment

        • ninjadev@gmail.com

          #5
          Re: Foreach element in $_POST?

          fun with forms... iterate_form() not only spits out the values, but
          will give you the $var = $_POST['var'] code for the receiving page...
          have fun

          function iterate_form()
          {
          echo "<b>Values: </b><br>";
          foreach($_POST as $key => $value)
          {
          echo $key . " = " . $value . "<br>";
          }
          echo "<br><br><b>Hap py Coding:</b><br>";
          foreach($_POST as $key => $value)
          {
          echo "$". $key . " = \$_POST['". $key . "'];<br>";
          }
          build_back_form ();
          exit;
          }

          function build_back_form ()
          {
          echo "<form name='error' method='post' action='".
          $_SERVER['HTTP_REFERER'] ."'>\n";
          foreach ($_POST as $key => $value)
          {
          echo "<input type='hidden' name='" . $key . "' value='" .
          $value . "'>\n";
          }
          echo "<input type='submit' name='submit' value='Go
          Back'></form>\n";
          }

          Comment

          Working...