I have been trying to do this and i dont know why exactly its not working.. Here's the idea.
I want to receive the name and age of a person via POST method and then put it in an array dynamically, so that as more names and ages are added, the array is increased.

<?php
if (isset($_POST['submit'])){
    $name= $_POST['name'];
    $age= $_POST['age'];

    foreach($array as $name=>$age){
        $array[$name]= $age;
    }


print_r($array);

}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
 <form method="post" action=" <?php $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="name"/>
age:<input type="text" name="age" />
<input type="submit" name="submit" value="Add to Array" />
</form> 
</body>
</html>

A PHP array is stored in memory and only exists for the duration that it takes for the individual web page to render. If you want to keep adding to it, and not have that information be volatile (but actually retained), you're going to want to be storing it in a database, not just in RAM.

However, there is one way to do it, and that's to have a hidden form element that includes everything in the array up until that point.

In other words, not only will we be collecting Name and Age from text boxes, but we'll also have two hidden form elements for all of the previous Names and Ages up until this point.

Then, when doing hte form processing, we'll be adding the latest Name and Age to the list of all the previous ones, which we'll be passing back as yet another form element when the form loads up again.

And then repeat the cycle.

This will allow you to have a form that you can continuously submit to, and it will show the values from all of the previous form submissions immediately prior, but all the data will be lost as soon as you navigate your web browser away from the page. If you want the information to be retained, you need to store it in a database like MySQL.

Member Avatar for diafol

Or you could use a session array?

While the OP didn't say whether his app was using sessions or not I would definitely reccomend storing the names in session or in the cookie. If neither option is available then you would have to do some kind of workaround such as what Dani suggested.

As the previous members suggested, something like this for example:

<?php
if (isset($_POST['submit'])){
      $name= $_POST['name'];
      $age= $_POST['age'];
      foreach($array as $name=>$age){
      // After proper sanitising of course :P
           $_SESSION['my_values'][$name]= $age;
      }
      print_r($_SESSION['my_values']);
}
?>

I am but a poor noob, please forgive me if the formatting isn't right... :)

@Gaetane don't forget session_start() at the top of the page!

My bad :P I develop through a framework that does all the grunt work for me and have slipped into bad ways!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.