-1

php newbie here. How do I retain the value of an array when I click the submit button? So this how the program works. The user will input a name and a seat number(between 1-25) and when the the user clicks submit button, the name will be added on the array and will be displayed on the table. It works for the first input but when i entered another name and seat number, the first input is deleted. Please help.

     //this is where the user will input...
     Student Name: <input type="text" name="name" id="name"><br>
     Seat Number: &nbsp;<input type="text" name="seat" id="seat"><br>
    <input type="submit" name="assign" value="Assign"> <?php echo $warning; ?>

    //and this is my php code.
    <?php
        $students = array_fill(0, 25, NULL);//this is to make an empty array with the size of 25.
        if(isset($_POST['assign'])){
            $student = $_POST['name'];
            $seat = $_POST['seat'];

            if($seat > 25 || $seat < 1){
                $warning = 'Seat number does not exist.';
            }
            else{
                $warning = '';
                $students[$seat-1] = $student;
            }
        }
    ?>

    //This code here is just a part of the HTML code for the table.  this is where the name will display.
    <td id="box"><?php echo $students[0]; ?></td>
    <td id="box"><?php echo $students[1]; ?></td>
    <td id="box"><?php echo $students[2]; ?></td>
    <td id="box"><?php echo $students[3]; ?></td>
    <td id="box"><?php echo $students[4]; ?></td>
Kev Nish
  • 11
  • 2
  • 8

2 Answers2

0

Because every time you click on submit button,the $students array is created again and again and therefore you lose the previous values which the $students was holding.So create your array only once while submitting your form the first time.
EDIT: alternative: use php SESSION for this(just one of the many alternatives)
Before you can store user information in your PHP session, you must first start up the session.

<?php session_start(); ?>

<html>
<body>

</body> 
</html>

And then you can store or retrieve anything in the session by manipulating $_SESSION array in php.So instead of storing seat no.s in a normal php variable you can use $_SESSION.Just keep everything the same(ie method="post" etc etc) and instead of $students use $_SESSION["students],where students will be an array in the $_SESSION(and it will remain there till your session expires ie till user logs out or closes the page in your case)

See here for more example: http://www.w3schools.com/php/php_sessions.asp

Anmol
  • 303
  • 2
  • 14
0

Use sessions like this:

<?php
session_start(); // To start the session, must be called before trying to manipulate any session variables. Preferably the first php-line on each page who will use sessions.

$_SESSION['students'] = array_fill(0, 25, NULL); // Sets an array of 25 indexes to session variable 'students'
?>

The code block above should not be called each time the user clicks submit - it should only be called once.

<?php

session_start();

if(isset($_POST['assign'])){
   $student = $_POST['name'];
   $seat = $_POST['seat'];

        if($seat > 25 || $seat < 1){
            $warning = 'Seat number does not exist.';
        }
        else{
            $warning = '';
            $_SESSION['students'][$seat-1] = $student; // Sets index [$seat-1] of array to $student
        }
    }
?>

Session variables are stored temporarily on the server and can be accessed throughout the website as long as you remember to start the session on each page (as shown in the code).

As Anmol said before, read more about sessions here: http://www.w3schools.com/php/php_sessions.asp

Edit:

<?php
session_start(); // To start the session, must be called before trying to manipulate any session variables. Preferably the first php-line on each page who will use sessions.

if(!isset($_SESSION['students'])){
    $_SESSION['students'] = array_fill(0, 25, NULL); // Sets an array of 25 indexes to session variable 'students'
}
?>
simeg
  • 1,889
  • 2
  • 26
  • 34
  • Uhmm, i still get a problem. did i do it correctly? i put the first code block before the html tags and the second one inside the html. like this: `first php second php ` – Kev Nish Jul 13 '14 at 15:29
  • I assume you have all the code in one single page. Therefore you should check to see if the session variable 'students' exists before you "empty" it by setting it to an empty array. See my edit. – simeg Jul 13 '14 at 18:39