Need help, adding values into arrays from forms
Posted: Fri Jan 02, 2009 10:17 am
So I have been looking around for a solution to my problem but can't think come up with anything. At the moment I have an multi-dimensional associative array(I think thats what you call it) with some start values in it. And there is a form adding an one new array in the lastspot in my multi-dimensional array. The problem is I want the script to be able to add many values. But at the moment it just replaces the old value.
Also I am trying to save the values in the arrays using sessions. The written person in the form isnt saved when the page is reloaded. So two problems basically, adding more persons using the form and the session problem.
This is just a training for me, since I am pretty new to php. So this might be a really easy solution I havent thinked of. The code has english mixed with swedish variable names. So it might be confusing for some. Anyway here is the code:
Thanks in advance 
Also I am trying to save the values in the arrays using sessions. The written person in the form isnt saved when the page is reloaded. So two problems basically, adding more persons using the form and the session problem.
This is just a training for me, since I am pretty new to php. So this might be a really easy solution I havent thinked of. The code has english mixed with swedish variable names. So it might be confusing for some. Anyway here is the code:
Code: Select all
<?php
session_start();
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Klasslista</title>
<style type="text/css" media="screen">
td{
border: #000000 1px solid;
}
</style>
</head>
<body>
<?php
$_SESSION['people'] = array(
array(
"first_name" => "David",
"last_name" => "Cakeman",
"alder" => 21,
"telefon" => "048224455"
),
array(
"first_name" => "Daniel",
"last_name" => "Mancake",
"alder" => 22,
"telefon" => "04823355"
),
array(
"first_name" => "Donald",
"last_name" => "Duck",
"alder" => 31,
"telefon" => "048555455"
)
);
if($_POST["forname"] && $_POST["eftername"] && $_POST["age"] && $_POST["telefon"]){
$_SESSION['new_person'] = array(
"first_name" => $_POST["forname"],
"last_name" => $_POST["eftername"],
"alder" => $_POST["age"],
"telefon" => $_POST["telefon"]
);
$_SESSION['people'][] = $_SESSION['new_person'];
}
?>
<form action="" method="post">
Firstname: <input type="text" name="forname" />
Lastnamn: <input type="text" name="eftername" />
Age: <input type="text" name="age" />
Telephone: <input type="text" name="telefon" />
<input type="submit" value="Skicka" />
</form>
<?php
print_r($_SESSION['people']);
echo "<br />";
echo "<table>";
foreach($_SESSION['people'] as $person){
echo "<tr>";
echo "<td> {$person[first_name]} {$person[last_name]}</td>";
echo "<td> {$person[alder]} år</td>";
echo "<td> {$person[telefon]} </td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>