Hey, I'm trying to follow this tutorial to learn PHP. Its a pretty good tutorial; more than once up until this point, I've had to modify the script he gave me because it wasn't working. But I've been able to figure it out. But now I've hit a snag that I can't figure out. I'm ready to bite off my lips and chew them until the flavor runs out to get my mind off of this script. Here it is, straight off the page:

<html>

<body>

<?php

if ($submit) {

// process form

while (list($name, $value) = each($HTTP_POST_VARS)) {

echo "$name = $value<br>\n";

}

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

First name:<input type="Text" name="first"><br>

Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php

} // end if

?>

</body>

</html>

And when I try to run it, I get this:
Notice: Undefined variable: submit in C:\Program Files\Apache Group\Apache2\htdocs\newuser.php on line 14 as well as the forms.

And when I try to submit, I get this:
Not Found
The requested URL /<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>C:/Program Files/Apache Group/Apache2/htdocs/newuser.php</b> on line <b>30</b><br /> was not found on this server.


Apache/2.0.40 Server at localhost Port 80

I have apache and PHP 4.something installed. I have mysql installed, but that shouldn't affect anything for this script. I'm just putting stuff in the default main directory that comes with apache and running it localhost/x.php (for instance, this page is localhost/newuser.php).

Now, PHP DOES work on my machine, at least for some things. A simple script like this:
<?php

$myvar = "Hello World";

echo $myvar;

?>

Works fine. Any idea why this script here doesn't work? I like this tutorial, it makes sense to me, I can follow it, but the non-working scripts are killing me. Can someone hook me up with a similar skill-level tutorial? Thanks for any help πŸ™‚

    try this:

    <?
    if(isset($_POST['submit']))
    {
     foreach($_POST['info'] as $k=>$v)
     {
      echo "$k = $v<br>\n";
     }
    }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> 
    <?
    }
    ?>
    

    i didnt test it, but i think that should work

    what you were doing wrong was :
    you were looping through an array, yet it wasnt submitted in an array, the way you had it setup was that say, "first" would = $_POST['first'] and not an array element (hope that made sense)

      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...