View Full Version : php sessions

11-18-2012, 12:20 PM
Hi i have a html page with a simple form and i want to be able to input only INTS, and after i press submit i wana be on the same page and the value gets stored in an array, until some1 enters zero and it will then show how many ints you have entered. i think the it should be done with sessions, here is my code:



<form action="test.php" method="post">
Heltal: <input type="text" name="tal" />
Heltal: <input type="text" name="tal2" />

<input type="submit" />





$tal = array ($_POST['tal'],$_POST['tal2']);

foreach ($tal as $koll => $value) {
if (ctype_digit($value)) {
echo "The string $koll consists of all digits."."</br>";
} else {
echo "The string $koll does not consist of all digits."."</br>";

$sum = array_sum($tal);
echo "Summan:".$sum."</br>";

$antal = count($tal);
echo "Antalet tal: ".$antal."</br>";

$medel = $sum/$antal;
echo "Medelvärdet: ". $medel;


11-18-2012, 01:42 PM
yep your right. this can be done with sessions. do you no how to use sessions in php?

also just a quick tip. if you wanna send an array of values from a form, give the elements the same name suffixed with []

Heltal: <input type="text" name="tal[]" />
Heltal: <input type="text" name="tal[]" />and then in your php

foreach($_POST['tal'] as $val){

//do something with each $val here