Ok it does work on that page, but when I submit, it says

Notice: Undefined variable: submit in C:\Program Files\Apache Group\Apache2\htdocs\userconfirm.php on line 12 and shows me the forms again. Before I get to that question, I'd like to know something. What are you doing with this line of code:

foreach($_POST['info']

How does that work? What is that expression saying? My problem, I guess, is that I don't get how $POST works. What does $POST['info'] mean?

Also, how does foreach work? I've looked them both up in the php.net lexicon, and I don't quite understand their definitions either πŸ™‚

Oh yea, and finally, what does "as $k=>$v" mean?

    1. $POST is the exact same as $HTTP_POST_VARS, only $POST is the 'newer' version of it.. both work the same. this is just an array of all the form elements (by name) and by checking for $POST['info'] youre loading the info[0],info[1],info[2] array.. so now $arr=$POST['info'] == $info[0],[1],[2].. it works for everything else too, like:
      <input type=text name='firstname'>
      this would = $_POST['firstname'] in php

    note:
    if you dunno what i mean still, try using one of these:
    var_dump($POST);
    or
    print_r($
    POST);

    1. the foreach loop is perfect for this loop. what it does is basically take the FOR statement (for($i=0;$i<count($_POST['info']);$i++) and shorten(in a way) it just goes through each array element and lists it as $k=>$v (key => value - you can replace $k and $v with any vars you want) in your case the key didnt really matter since it was just a 0,1,2 .. but in arrays like:
      $arr=array("one"=>"onevalue", "two"=>"twovalue"); the foreach statement would loop 2 times, the first time $k would = one, and $v would = onevalue.. on the 2nd loop $k would = two, etc..etc..

    note:
    its exactly like this, but i see 'foreach' as a shorthand way
    while(list($k,$v) = each($_POST['info']))
    {
    echo $k . " => " . $v . "<br>";
    }

    hope that clarified it a little πŸ™‚

      Your script doesn't work, otherwise I would try var_dump($_POST); πŸ˜‰

      I think I understand what $_POST is. Could you explain in non-coding language the rest of you script? Not the forms part, just those few lines?

      Thanks for your help, I don't want to monopolize your time though πŸ™‚

        Originally posted by brΓΈken

        <?
        // check if the submit button was clicked
        if(isset($_POST['submit']))
        {
         // it was, so loop through the infoarray
         // it contains elements: first,last,address,position
         foreach($_POST['info'] as $k=>$v)
         {
          // echo each listing $k=>$v
          // $k would = first,last,address,position
          // $v would = their values
          echo "$k = $v<br>\n";
         }
        // there was no post vars passed, so print the form
        }else{
        ?>
        <form method="post" action="<?php echo $PHP_SELF?>"> 
        First name:<input type="Text" name="info[first]'"><br> 
        Last name:<input type="Text" name="info[last]"><br> 
        Address:<input type="Text" name="info[address]"><br> 
        Position:<input type="Text" name="info[position]"><br> 
        <input type="Submit" name="submit" value="Enter information"> 
        </form> 
        <?
        }
        ?>
        

          if the submit still isnt found,

          replace this:
          if(isset($_POST['submit']))

          with this:
          if($_POST)

            I think your having the same problem that i was having with var.s when i installed php on my machine, they work if they are defined in the php page but when comeing form another page they play up. check your php info and look to see if register_globals (under the php core settings) is on or off. if it is off you will need to change the php.ini, which is usually should be c:\windows\php.ini. find the register_globals and turn it on (register_globals = On), I also changed the order of the variables when i changed this so i don't know if this help effect the change (variables_order = "GPCES").

            hope this solves your problem.

            πŸ˜ƒ

              
              // PAGE2.PHP //
              <?
              if(isset($GLOBALS['submit']))
              {
               foreach($GLOBALS['info'] as $k=>$v)
               {
                echo "$k = $v<br>\n";
               }
              }
              ?>
              // PAGE1.PHP //
              
              <form method="post" action="page2.php"> 
              First name:<input type="Text" name="info[first]'"><br> 
              Last name:<input type="Text" name="info[last]"><br> 
              Address:<input type="Text" name="info[address]"><br> 
              Position:<input type="Text" name="info[position]"><br> 
              <input type="Submit" name="submit" value="Enter information"> 
              </form> 
              
              

              if that doesnt work, i dunno what to tell you? either $_POST or $GLOBALS work on my machine, so i really have no way to test more things for u, sry

                It works! Hallejuiah! Thanks guys πŸ™‚. Just one question, I get that "echo $k = $v<br>\n" is telling it to display the key and its value for each piece of info in the $_POST array. But... what does the

                as $k=>$v

                statement do?

                P.S. I'm going through now and finding out what the list() and each() functions do

                  when you make an array like $a=array("hello"=>"world");

                  $k = hello, and $v = world πŸ™‚ its setup just like the array as $k=>$v

                  the same as while(list($k,$v)=each($a))

                    Originally posted by idleyouth
                    I think your having the same problem that i was having with var.s when i installed php on my machine, they work if they are defined in the php page but when comeing form another page they play up. check your php info and look to see if register_globals (under the php core settings) is on or off. if it is off you will need to change the php.ini, which is usually should be c:\windows\php.ini. find the register_globals and turn it on (register_globals = On), I also changed the order of the variables when i changed this so i don't know if this help effect the change (variables_order = "GPCES").

                    hope this solves your problem.


                    πŸ˜ƒ

                    Thanks for THIS post. I really thought that I was going CRAZY. Sure enough, it worked. I don't recall ANY past PHP installs where this had to be modified. But sure enough.

                      a year later

                      check your php info and look to see if register_globals (under the php core settings) is on or off.

                      I found that I was getting an 'Undefined variable' message when I was running a simple script under localhost, but on uploading my script to an Internet server, the error disappeared.

                      The solution was to edit php.ini (which I found in my /windows folder) and changing the following line as follows:

                      register_globals = On

                      It seems that if this is set to Off, then you can not access form variable posts.

                      Regards,
                      Ian

                        btw, brΓΈken, $POST differs from $HTTP_POST_VARS in that $POST is a superglobal, while $HTTP_POST_VARS has to be declared global in functions.

                          Write a Reply...