How to assign a php variable to an html radio button value?

Hi, after banging my head on the desk for about a week now I’ve given in that fact that I know nothing and need help (as simple as it may seem to others)

Basically (or evidently not so basic) i’m trying to create a random question generator (in php with a mysql db) with radio button answer selection (four choices).

So far so good, I can get the question and answers to display as desired by using the method below however, I cannot figure/find out how to assign the potential answer/s (as a variable) to the value of the radio button. With a view to recording the selected answer in a second database.

I recognise this code may be fundamentally flawed and accept there may be other more efficient and elegant ways of achieving the same thing, these aren’t always apparent when trying to teach ones self.

Some help would be most welcome and gratefully accepted if you anyone has the time.

Please find a bare bones example of code below for your perusal and hopefully in some way an explanation of what I am trying to achieve.

Aplogies in advance for erroneous and ill placed posting, if I have.

I will temporarily stop banging head on desk in anticipation of a helpful reply.

include ‘db1.php’;

for ($a=0; $a < 5; $a++)
$random = (rand()%5)+1;
$q = $random;

$result = mysql_query(“SELECT * FROM qdb”);
if (!$result)
exit (‘<p>Error accessing database !<br />’.
'error: ’ . mysql_error(). ‘</p>’);

//Question selection
$r1 = mysql_query(“select q from qdb where id = ‘$q[0]’”);

//Answer one selection
$z1 = mysql_query(“select a1 from qdb where id = ‘$q[0]’”);

//Question one answer display
$q1a1 = mysql_fetch_array($z1);


<form action = “bt.php” method=“POST”>
<input type=“radio” name=“fred” value= " " > <? echo $q1a1[a1]; ?> <p>
<input type=“radio” name=“fred” value= “two” > two </p>
<input type=“radio” name=“fred” value= “three” > three </p>
<input type=“radio” name=“fred” value= “four” > four <p>
<button value=“Go” type=“submit”>Submit</button></p>


Hi jominsons, welcome to the forums, (posting, that is)
You can assign PHP var vals the same way that’s in your example. eg.

<input type="radio" name="fred" value= "<? echo $q1a1[a1]; ?>" > <? echo $q1a1[a1]; ?> <p>

But unless you create additional fields for your database table, assigning the answer to the radio values will cause problems, if it works at all, not to mention being rather messy. I would create a table something like this with 2 fields for every answer. 1 for an id “value” and 1 for the text
Q1 text, A1 “id”, A1 text, A2 “id”, A2 text …
By id I don’t mean the table row id, but just a way to link the answers to something simple to put in the radio value.

Hi jominsons,

I noticed that rest of the three optional answers are not coming from the database. So my guess is that you have only one answer (which is the correct one) in your answers’ table. Also I couldnt find the code that randomly prints the options.

You might wish to add four optional answers (one answer flagged to be the correct answer) for each question in the database and fetch them randomly and just print it :slight_smile:

And as Mittineague said, use the unique id of answer as the value of radio button on your HTML form.

Let us know if you need further help.

Hi guys, many thanks for the quick responses, much appreciated.

Mittineague’s idea for the variable name seems to work as I wished. I just need to test what will be entered into the database that records the answers. (so I may be back posting after some more head bashing !)

I’m sure there are better & easier ways of achieving what I am trying to do but unfortunately I’m stumbling along via forums and web searches.

Ihtesham, all the answers will come from a database (with a question, four possible answers and one field that identifies the correct answer) and the users email address, answers, time taken etc will be recorded in another. I had used the other radio button values in the code above to establish if I had the $_POST function working correctly. I apologise if my post was misleading.

Many, many thanks for the help and I’m sure I will be back posting at some point.

Have a good one.

Hi again, Just tried entering the results in the database and all seems fine (so far).

Once more, thank you.

That sounds great jominsons.

You are welcome.