/    Sign up×
Community /Pin to ProfileBookmark

Displaying answers from forms with radio buttons

Hi,

I have made a form with radio buttons which i think is correct. There is a true and a false radio button for each part. Then a submit at the bottom.

How do i display the selected answer on the next page.

<html>
<head>
<title>Practice Exam in AERN</title>
</head>

<body bgcolor=”#8080FF”>
<font face=”Arial Rounded MT Bold” color=”#000000″>

<form action=”answer.php” method=”post”>
<table border=0>

<tr>
<td width=150>Question<br>
</td>
<td width=15>True<br>
</td>
<td width=15>False<br>
</td>
</tr>
<tr>
<td>1 a</td>
<td align=”center”><input type=”radio” name=”1a” value=”1atrue”></td>
<td align=”center”><input type=”radio” name=”1a” value=”1afalse”></td>
</tr>
<tr>
<td>1 b</td>
<td align=”center”><input type=”radio” name=”1b” value=”1btrue”></td>
<td align=”center”><input type=”radio” name=”1b” value=”1bfalse”></td>
</tr>
<tr>
<td>1 c</td>
<td align=”center”><input type=”radio” name=”1c” value=”1ctrue”></td>
<td align=”center”><input type=”radio” name=”1c” value=”1cfalse”></td>
</tr>
<tr>
<td colspan=”2″ align=”center”><br>
<input type=”submit” value=”Submit Answer”></td>
</tr>
</table>

</form>

</font>
</body>
</html>

On the answer.php page i would like to have a table with

1a – answer selected
1b – answer selected
1c – answer selected

How should i go about doing this?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 05.2005 — Change those values to just plain [b]true[/b] and [b]false[/b]. Then, you can use this:
[code=php]<?php
echo '<p>1a - '.$_POST['1a']."<br>n";
echo '1b - '.$_POST['1b']."<br>n";
echo '1c - '.$_POST['1c']."</p>n";
?>[/code]
Copy linkTweet thisAlerts:
@RAHauthorApr 05.2005 — Thats great. Thanks a lot. I was trying something else, but it didnt work. Yours did ?

OK. So i have 3 questions, each with a true or false answer. And this has printed the answer onto the next page.

If i wanted to compare the answer chosen to a "correct" answer, would it be much harder? If, for instance, i wanted the number of answers that matched the correct answers printed at the bottom?
Copy linkTweet thisAlerts:
@NogDogApr 05.2005 — [code=php]
# array of correct answers:
$answers = array (
"1a" => TRUE,
"1b" => FALSE,
"1c" => TRUE
);
# initialize count:
$numCorrect = 0;
# check each answer:
foreach($answers as $key => $value)
{
if($value === $_POST[$key])
{
$numCorrect++;
}
}
echo "<p>You got $numCorrect correct answers out of " . count($answers) . " questions.</p>n";
[/code]
Copy linkTweet thisAlerts:
@RAHauthorApr 06.2005 — It doesnt seem to work properly when i use that piece of code. When its uploaded, no matter what answers you put in on the form page, it always says you have 0 out of 3.

http://rah.t35.com/question.html

I dont think it is adding 1 to the $numCorrect for each correct answer.

Any ideas?
Copy linkTweet thisAlerts:
@NogDogApr 06.2005 — Try changing the TRUE/FALSE values in the $answers array to strings that exactly match the form values, something like:
[code=php]
$answers = array (
"1a" => "true",
"1b" => "false",
"1c" => "true"
);
[/code]
Copy linkTweet thisAlerts:
@RAHauthorApr 06.2005 — Ah, thats it. In the array they needed to be in "" marks. Thanks a lot.
×

Success!

Help @RAH spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...