Reputation: 497
Could anyone explain me how to solve the following:
function GetSetClearForm(){
$person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
print_r($person);
$personlist = array();
array_push($personlist,$person);
print_r($personlist);
return $personlist;
}
When the print_r($personlist);
has ran I get the following output:
Array (
[0] => Array (
[firstname] => 2
[lastname] => 2
[age] => 2
[city] => 2
[zipcode] => 2
[address] => 2
)
)
(filled in all textboxes with "2").
This is ok at this point, but whenever I fill in another one I get this output:
Array (
[0] => Array (
[firstname] => 1
[lastname] => 1
[age] => 1
[city] => 1
[zipcode] => 1
[address] => 1
)
)
(filled in all textboxes with "1").
So instead of creating another person on a new index (index[1]
) it replaces index[0]
with a new person and deletes the older one. I dont want it do delete it I want to get a list full of people. I think it has to do with the array_push
but I am not sure I hope anyone could help me out here.
EDIT:
Added the index.php:
<form action="check.php" method="POST">
<table>
<tr><td>First name</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last name</td><td><input type="text" name="lname"></td></tr>
<tr><td>Age</td><td><input type="text" name="age"></td></tr>
<tr><td>City</td><td><input type="text" name="city"></td></tr>
<tr><td>Zipcode</td><td><input type="text" name="zcode"></td></tr>
<tr><td>Adress</td><td><input type="text" name="address"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
Added the check.php
<?php
include("functions.php");
$personlist = array();
$personlist[] = GetSetClearForm();
print_r($personlist);
?>
Upvotes: 1
Views: 475
Reputation: 747
I think the problem is you are re-creating person list each time the function is called. Maybe you can solve it like this:
//Create your person list some where else and keep it alive.
//Like in a session?
session_start();
//Then give your keep alive array to your function each time.
function GetSetClearForm(){
$person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
print_r($person);
return $person;
}
$_SESSION['personlist'][] = GetSetClearForm() ;
print_r( $_SESSION['personlist']);
Upvotes: 0
Reputation: 2018
Instead of using array_push you can use this:
$personlist = array();
$personlist[] = $person;
This way a new index is created every time you 'insert' a new value, in this case an array.
EDIT:
You need to declare and fill your $personlist array outside your function. That said:
function GetSetClearForm(){
$person = array('firstname' => $_POST["fname"], 'lastname' => $_POST["lname"],'age' => $_POST["age"] ,'city' => $_POST["city"] ,'zipcode' => $_POST["zcode"],'address' => $_POST["address"]);
print_r($person);
return $person;
}
$personlist = array();
$personlist[] = GetSetClearForm();
Upvotes: 2