0

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>
6
Contributors
6
Replies
36
Views
3 Years
Discussion Span
Last Post by Gaetane
1

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.

1

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.

0

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... :)

Edited by Gaetane: Didn't refactor properly

0

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

Edited by Gaetane: Deletion

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.