Radio button value - use as variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Radio button value - use as variable

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Radio button value - use as variable

Post 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.
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Radio button value - use as variable

Post by mikes1471 »

Oh just the blindingly obvious then!! lol sorry!!! :roll:
Post Reply