trouble with arrays + sessions

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

    trouble with arrays + sessions

    I just started with php and I'm trying to make a simple interface as
    follows:

    - user is asked to input an integers, for example: how many students do
    you want to enter?
    - user is then shown a page with number of text boxes = number he gave
    at the previous page
    - user fills out the test boxes with names of students and clicks
    submit
    - the user is sent to another page where the above names are output to
    the screen

    here is the code for the relevant pages:
    First i ask for how many text boxes the user would like...
    =============== =============== =============== =============== ==========
    <?
    session_start() ;
    ?>

    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>ECSLAB Submit System Course Registration</title>
    </head>

    <body>
    <table>
    <tr>
    <td>
    <p>
    <?
    if($_SESSION['sizeerror']) {
    echo "Inputs must be
    integers<br><br >";
    $_SESSION['sizeerror'] =
    0;
    }
    ?>
    <form method="POST"
    action="transit ion.php">
    How many instructors (TAs,
    professors, etc) will need access to the system?<br>
    <input type="text"
    name="numinstru ctors" size="20"><br>< br>
    How many students would you like
    to add?<br>
    <input type="text"
    name="numstuden ts" size="20"><br>< br><br>
    <input type="submit"
    value="Submit" name="B1">
    </form>
    </p>
    </td>
    </tr>
    </table>
    </body>

    </html>
    =============== =============== =============== =============== ==============



    if the input was correct (both were integers) you get redirected to the
    next page...this part works fine (or so it seems). it displays the
    correct number of text-boxes...
    =============== =============== =============== =============== ==============
    <?
    session_start() ;
    ?>
    <HTML>

    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>ECSLAB Submit System Course Registration</title>
    </head>

    <body>
    <table align="center">
    <form method="POST" action="submit. php">
    <tr>
    <p>
    Coursename:&nbs p;&nbsp;&nbsp;< input type="text"
    name="coursenam e" size="20"><br>< br>
    </p>
    </tr>
    <tr>
    <p>
    Instructor ECS Lab Accountnames:<b r><br>

    <?
    for( $k = 0; $k <
    $_SESSION['numinstructors ']; $k++){
    echo "<input type=\"text\"
    name=\"instruct or[$k]\" size=\"20\"><br ><br>";
    }
    ?>
    </p>
    </tr>
    <tr>
    <p>
    <br><br>Stude nt ECS Lab Accountnames:<b r><br>

    <?
    for( $i = 0; $i <
    $_SESSION['numstudents']; $i++ ){
    echo "<input type=\"text\"
    name=\"student[$i]\" size=\"20\"><br ><br>";
    }
    ?>

    <input type="submit" value="Submit" name="B1">
    </p>
    </tr>
    </form>
    </table>
    </body>

    </html>
    =============== =============== =============== =============== =============



    submitting the names sends you to the following page...
    =============== =============== =============== =============== ==============
    <?
    session_start() ;

    $coursename = $_POST['coursename'];
    $_SESSION['coursename'] = $coursename;

    for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
    $idx = 'student[' . $i . ']';
    $student[$i] = $_POST[$idx];
    $_SESSION[$idx] = $student[$i];
    }

    for( $k = 0; $k < $_SESSION['numinstructors ']; $k++ ){
    $instructor[$k] = $_POST["instructor[$k]"];
    $_SESSION["instructor[$k]"] = $instructor[$k];
    }
    ?>
    <HTML>

    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>ECSLAB Submit System Course Registration Successful</title>
    </head>

    <body>
    <?
    for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
    $x = 'student[' . $i . ']';
    echo $_SESSION[$x];
    echo "<br>";
    }
    echo "now instructors<br> ";
    for( $k = 0; $k < $_SESSION['numinstructors ']; $k++ ){
    $x = 'instructor[' . $k . ']';
    echo $_SESSION[$x];
    echo "<br>";
    }
    ?>
    </body>

    </HTML>
    =============== =============== =============== =============== ============


    i expect it to print something like:
    student1
    student2
    student3
    now instructors
    teacher1
    teacher2

    but instead it prints:



    now instructors


    (so, 3 blank lines, "now instructors", then 2 blank lines)
    Anyone have any idea what I'm doing wrong?


    Thanks in advance,
    --Lucas

  • BKDotCom

    #2
    Re: trouble with arrays + sessions

    ugh... where to begin...
    1st is this just a general "how do sessions work test"?
    because sessions appear to be completely unnecessary here.

    here's a working "transition.php "
    <?php
    session_start() ;
    ?>
    <HTML>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <title>ECSLAB Submit System Course Registration</title>
    </head>
    <body>
    <form method="POST" action="submit. php">
    Coursename:&nbs p;&nbsp;&nbsp;< input type="text" name="coursenam e"
    size="20"><br>< br>
    <p>
    Instructor ECS Lab Accountnames:<b r>
    <?php
    for( $k = 0; $k < $_POST['numinstructors ']; $k++)
    echo '<input type="text" name="instructo r['.$k.']" size="20"
    /><br/>'."\n";;
    ?>
    </p>
    <P>
    Student ECS Lab Accountnames:<b r>
    <?php
    for( $i = 0; $i < $_POST['numstudents']; $i++ )
    echo '<input type="text" name="student['.$i.']" size="20" /><br
    />'."\n";
    ?>
    <INPUT type="hidden" name="numinstru ctors"
    value="<?=$_POS T['numinstructors ']?>" />
    <INPUT type="hidden" name="numstuden ts"
    value="<?=$_POS T['numstudents']?>" />
    <?php
    // or, if you insist on using sessions, you need to set your session
    values here
    // they don't magically set themselves
    // $_SESSION['numinstructors '] = $_POST['numinstructors ']
    // $_SESSION['numstudents'] = $_POST['numstudents']
    ?>
    <input type="submit" value="Submit" name="B1">
    </p>
    </form>
    </body>

    </html>

    *************** *************** *****

    in submit.php you've got a few probs

    you can use:
    for( $i = 0; $i < $_POST['numstudents']; $i++ )
    // or $_SESSION['numstudents'] if you've defined it (you hadn't)
    {
    $name = 'student[' . $i . ']';
    $_SESSION[$idx] = $_POST[$name];
    }

    but down below you're echoing
    $_SESSION[1]
    should be
    $_SESSION['student[1]']
    at least the way you've set previous pages.

    and again, sessions weren't necessary... unless you're doing something
    with these value in later pages...

    Comment

    • LMachado1@gmail.com

      #3
      Re: trouble with arrays + sessions

      I actually do have a transition.php. I guess I should have included it
      instead of just assuming people would know that it is there. The page
      that you thought was not transition.php but actually register.php. The
      following is the page that should come between the first group of code
      I put up and the second....sorry for the confusion

      =============== =============== =============== =====
      <?
      session_start() ;

      $numstudents = $_POST['numstudents'];
      $_SESSION['numstudents'] = $numstudents;

      $numinstructors = $_POST['numinstructors '];
      $_SESSION['numinstructors '] = $numinstructors ;
      ?>
      <HTML>

      <head>
      <meta http-equiv="Content-Type" content="text/html;
      charset=windows-1252">
      <title>ECSLAB Submit System...</title>
      </head>

      <?
      if(ctype_digit( $numstudents) && ctype_digit($nu minstructors)) {
      echo "<meta http-equiv=\"refresh \"
      content=\"0;URL =http://node1\
      7/register.php\"> ";
      }else {
      $_SESSION['sizeerror'] = 1;
      echo "<meta http-equiv=\"refresh \"
      content=\"0;URL =http://node1\
      7/classsize.php\" >";
      }
      ?>

      <body>
      </body>

      </html>

      Comment

      • LMachado1@gmail.com

        #4
        Re: trouble with arrays + sessions

        I have modified the following lines:

        echo "<input type=\"text\" name=\"instruct or[$k]\"
        size=\"20\"><br ><br>";
        and
        echo "<input type=\"text\" name=\"student[$i]\" size=\"20\"><br ><br>";

        to
        echo '<input type="text" name="student['.$i.']" size="20"><br>' ;
        and
        echo '<input type="text" name="instructo r['.$k.']" size="20"><br>' ;

        like you said....I also modified my final page (where I'm trying to
        echo the names inputted by the user) to:

        for( $i = 0; $i < $_SESSION['numstudents']; $i++ ){
        $x = 'student[' . $i . ']';
        echo $_SESSION['student[1]'];
        echo "<br>";
        }
        echo "<br>now instructors<br> ";
        for( $k = 0; $k < $_SESSION['numinstructors ']; $k++ ){
        $x = 'instructor[' . $k . ']';
        echo $_SESSION['instructor[1]'];
        echo "<br>";
        }

        but it still does not print out any values...this leads me to believe
        that maybe the problem is in the page where I actually have the form
        for inputting the names into the arrays....Any ideas?

        At the same time i'm not sure why you said:[color=blue]
        >but down below you're echoing
        >$_SESSION[1]
        >should be
        >$_SESSION['student[1]']
        >at least the way you've set previous pages.[/color]

        I was fairly sure that the way I was printing it was infact trying to
        print:
        $_SESSION['student[1]'], $_SESSION['student[2]'], etc...since i had:

        $x = 'instructor[' . $k . ']';
        echo $_SESSION[$x];

        Because if I echo $x it prints: instructor[0], instructor[1], etc....

        Thanks
        --Lucas

        Comment

        Working...