0

I am working on a php script whereby I have to a form with 2 input fields, consisting of a name and id. When I input a name and a id, these are stored in an associative array. As long as an id is less or equal to 100, the names and ids are stored in an associative array. When an id greater than 100 is entered, the php script will display the names entered previously and their corresponding ids are displayed. However, when I enter the names and Ids, only one name and one Id is being displayed, and the current ones being replaced. I want a list of names and Ids to be displayed when I enter an Id greater than 100 irrespective of the name entered. See my codes below:

<?php

$id = 100;        

if(isset($_POST['lname']) && isset($_POST['id'])) {        


    $name = $_POST['lname'];
    $ids = $_POST['id'];

    while($GLOBALS['id'] <= 100) {

        $lists = array($name => $ids);
        foreach($lists as $key => $val) {
            echo $key . ' ' . $val . '<br>';
        }
 }   
}
?>

<form action = "index.php" method = "POST">

Name:<br>
<input type = "text" name = "lname"><br><br>
Marks:<br>
<input type = "text" name = "id"><br><br>
<input type = "submit" value = "Submit">

</form>
5
  • If you want to maintain data between requests to the server, then you need to store it somewhere persistent like session or a file or a database Commented Mar 27, 2015 at 13:59
  • Arrays won't work in this case ? Like as long as the Id is not greater than 100, both names and Ids are stored in an associative array. When an Id greater than 100 is entered, all the previous names and Ids entered are displayed on the browser. Commented Mar 27, 2015 at 14:37
  • An array is not persistent: yes, you will want an array, but you'll need to store that array somewhere between requests.... session is the obvious place to store it Commented Mar 27, 2015 at 14:45
  • Any hint on how to proceed ? Commented Mar 27, 2015 at 14:53
  • Starting point Commented Mar 27, 2015 at 15:07

1 Answer 1

1

You can use session to achieve what you want, see this code:

<?php  
session_start();

if(isset($_POST['lname']) && isset($_POST['marks'])){        

if($_POST['marks'] <= 100) {
$_SESSION['info'][] = array($_POST['lname'] => $_POST['marks']);
}
}

if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
  foreach($_SESSION['info'][$i] as $name => $marks){
    echo '<p>' . $name . '<br>';
    echo $marks . '</p>';
 }
} 
}
?>

<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">

Name:<br>
<input type = "text" name = "lname"><br><br>
Marks:<br>
<input type = "text" name = "marks"><br><br>
<input type = "submit" value = "Submit">

</form>

You should read some docs about session:

http://php.net/manual/en/intro.session.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.