PHP lessons

Hidden inputs

Average of a sequence of numbers

Back

There are two programs: average.php and average2.php

code of average.phpcode of average2.php
<?php

//the variables are setted to zero or to one
$sum=0;
$average=0;
$counter=1;

// The action is for the average2.php program
// the sum, average and counter variables
// are sent hiddenly with the assigned values

echo" Type in the first number
<form action='average2.php' method='post'>
<input name='number' size='6' type='text'>
<input name='sum' type='hidden' value='$sum'>
<input name='average' type='hidden' value='$average'>
<input name='counter' type='hidden' value='$counter'>
<input type='submit' value='Go!'>
</form>";

?>
<?php

//calculation
$sum = $sum + $number;
$average = $sum/$counter;
$average = round ($average,3);
$counter++;

// The action is for itself
// the sum, average and counter variables
// are sent hiddenly with the updated values

echo" <p>The current average is $average</p>
<p>Type in another number
<form action="average2.php" method=post>
<input name='number' size='6' type='text'>
<input name='sum' type='hidden' value=$sum>
<input name='average' type='hidden' value=$average>
<input name='counter' type='hidden' value=$counter>
<input type=submit value='Go!'></p>";
</form>

?>

Back

Valid HTML 4.01 Transitional