Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-18-2012, 11:20 AM   PM User | #1
omar1337
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
omar1337 is an unknown quantity at this point
php sessions

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:

html:

<html>
<body>

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

<input type="submit" />
</form>


</body>
</html>,



<html>
<body>




php:

<?php


$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;

?>
</body>
</html>
omar1337 is offline   Reply With Quote
Old 11-18-2012, 12:42 PM   PM User | #2
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
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 []

Code:
Heltal: <input type="text" name="tal[]" />
Heltal: <input type="text" name="tal[]" />
and then in your php
PHP Code:
foreach($_POST['tal'] as $val){

    
//do something with each $val here


minder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:13 AM.


Advertisement
Log in to turn off these ads.