I'm trying to pass the data that I input into the three fields generated by the script back to the script for processing in an array. This is my attempt, but I can't figure out why the elements don't have all the data that I inputed when I access them. Any pointers would be welcome.... Thank you

<?php
	session_start();
    if($_POST['submit'] == "submit") {


$foo = $_SESSION['pass'];

for ($a=0; $a<3; $a++)
{
	echo "echo $foo[$a] ";
}

}else
{
echo "CHECK BOX TEST PAGE";



echo "<form action=\"checkboxexample.php\" method=\"post\" >";
for ($a=0; $a<3; $a++)
{

	echo "<input type=\"text\" name=\"stuff[$a]\"></input>";

}
$_SESSION['pass']=$stuff;

echo "<input type=\"submit\" name=\"submit\" value=\"submit\"></input> </form>";
}

?>






    You're having trouble accessing

    echo "<input type=\"text\" name=\"stuff[$a]\"></input>";

    this array (within the for loop) using

        if($_POST['submit'] == "submit") {
    
    
    $foo = $_SESSION['pass'];
    
    for ($a=0; $a<3; $a++)
    {
        echo "echo $foo[$a] ";
    }
    
    }else

    that code? The above code (which is the only code that processes if the user has hit submit) is echoing the variable $foo, but in your form, you are setting a variable called stuff.

    Also

        for ($a=0; $a<3; $a++)
        {
            echo "<input type=\"text\" name=\"stuff[]\"></input>";
        }

    That would create an array called $stuff (no need to specify the numbers). Then to call that variable, use

    for ($i=0; i < count($stuff); $i++) {
       echo $stuff[$i] . "<br>\n";
    }

    This method is more "dynamic" so to speak becuase it counts the array $stuff and runs the for loop while you still have values, rather than using a set number of times.

    Just a suggestion, it comes in very handy.

    Cgraz

      It looks like you are confusing data posted by the form with passing data by means of the session.

      The first time your script is run it outputs the form, and the statment:
      $_SESSION['pass']=$stuff;
      which set's the session variable 'pass' to $stuff, which at this point is undefined.

      When you submit the form, the script is run again.
      ($POST['submit'] == "submit") is now true
      Then $foo is set to $
      SESSION['pass'] which contains 'undefined' (or somthing erronious).

      $stuff now contains the array of values you entered (if register_globals is set, otherwise use $POST['stuff']), but the line $SESSION['pass']=$stuff is now not executed.

        The first time your script is run it outputs the form, and the statment:$_SESSION['pass']=$stuff;
        which set's the session variable 'pass' to $stuff, which at this point is undefined.

        How is $stuff undefined if I created an arrray such that $stuff[0]="stuff in field1" $stuff[1]="stuff in field2....etc:

        for ($a=0; $a<3; $a++)

        {
        
        
        
            echo "<input type=\"text\" name=\"stuff[$a]\"></input>";
        
        
        
        }

          If you've got POSTed form fields stuff[0] stuff[1], etc. then they'll be available in the next page (after the form submission) as $POST['stuff'][0], $POST['stuff'][1], etc. and the entire stuff array as $_POST['stuff'].

          The $stuff[] array probably will be undefined; since 4.1, PHP doesn't automatically set those variables. That's why we have $_POST.

            How is $stuff undefined if I created an arrray such that $stuff[0]="stuff in field1" $stuff[1]="stuff in field2....etc:

            You did no such thing. You printed a form with fields named suff[0], stuff[1] ....

            That array is created after the form is submited, when your script is run the 2nd time.

              Originally posted by Code_guy


              You did no such thing. You printed a form with fields named suff[0], stuff[1] ....

              That array is created after the form is submited, when your script is run the 2nd time.

              How would I go about doing what I intended on doing?

                I think the real problem is in these few lines of code....

                What I intended on doing was creating an array named $stuff[] that would make $stuff[0]="whatever is in field1" $stuff[$a]="whatever is in field$a"

                for ($a=0; $a<3; $a++)
                
                {
                
                    echo "<input type=\"text\" name=\"stuff[]\"></input>";
                
                }
                  <?php
                      if ($_SERVER['REQUEST_METHOD'] == "POST") {
                  
                  for ($a=0; $a<count($_POST['stuff']); $a++)
                  {
                      echo $_POST[stuff][$a] . "<br>";
                  }
                  
                  }else
                  {
                  echo "CHECK BOX TEST PAGE";
                  
                  
                  
                  echo "<form action=\"checkboxexample.php\" method=\"post\" >";
                  for ($a=0; $a<3; $a++)
                  {
                  
                      echo "<input type=\"text\" name=\"stuff[$a]\"></input>";
                  
                  }
                  
                  echo "<input type=\"submit\" name=\"submit\" value=\"submit\"></input> </form>";
                  }
                  
                  ?>
                  
                  

                  you can use just $stuff if the PHP directive register_globals = on, but weedpacket is right the manual states:

                  // As of PHP 4.2.0 the default value of register_globals = off.
                  // Using/relying on this method is not preferred.

                    I'm trying to do it with register_globals off.

                      Then you also need to use:

                      $_SERVER['REQUEST_METHOD']

                      instead of $REQUEST_METHOD

                      I'm used to having it on 😃

                      crap,

                      also it should be:
                      echo $_POST['stuff'][$a] . "<br>";

                        Actually your code worked well Code_guy, but it didn't work for strings or decimals.... I could only get integers through.

                          Write a Reply...