Page 1 of 1

Radio button value - use as variable

Posted: Wed Jul 22, 2009 6:29 pm
by mikes1471
Hi guys

I am looking to create an average of the answers submitted by visitors using radio buttons. I have a simple script to work out an average but do not seem to be able to apply it to my form, the frustrating thing is I'm sure I am close with my efforts but that counts for nothing if you dont get the end result you want lol >>

Code: Select all

<body>
<form action="age.php" method="post" name="ageform">
<label>1
  <input type="radio" name="ageform" id="radio" value="19" onclick="this.form.submit();"/>
</label>
<label>2
  <input type="radio" name="ageform" id="radio" value="20" onclick="this.form.submit();"/>
</label>
<label>3
  <input type="radio" name="ageform" id="radio" value="21" onclick="this.form.submit();"/>
</label>
</form>
On this same page I have the following code:

Code: Select all

<?
$currentage = 7;
$sum = $currentage + $ageform;
$result = $sum/2;
echo($result);
?>
The result is showing as 3.5 so obviously I'm only able to echo the result of 7 / 2 and am not gathering the $ageform value. Also, is it OK to have the two codes on the same page? I have onclick="this.form.submit();" which returns to this page and gives an average of your answer over two guesses in this example, can someone point out where I am going wrong please?

Re: Radio button value - use as variable

Posted: Wed Jul 22, 2009 6:38 pm
by requinix
You have this $ageform variable but you never told PHP what value it should have.

POSTed form data does into the $_POST superglobal array. $_POST["name"] will be the value from the "name" field in your form.

Re: Radio button value - use as variable

Posted: Wed Jul 22, 2009 6:40 pm
by mikes1471
Oh just the blindingly obvious then!! lol sorry!!! :roll: