Thursday, 15 August 2013

Making only one radio button selectable with PHP when not in a form -



Making only one radio button selectable with PHP when not in a form -

i've been working on project school create mini-lesson website quizzes users on subject of our choosing. questions, choices, , reply must in 1 array , can displayed question , radio button selection. point , working on 1 right until have completed it. problem having right radio buttons selectable. how create 1 radio button per question selectable?

here code:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> </head> <body> <?php $score = 0; //multi-dimensional arrays questions, selections, , answers $questions = array( array('question' => 'which optional making easy fudge?', 'choice 1' => 'nuts', 'choice 2' => 'condensed milk', 'choice 3' => 'semi-sweet morsels', 'choice 4' => 'bakers chocolate', 'answer' => 'nuts'), ); //looping through questions nested loops foreach($questions $question){ echo $question['question'] . '<br />'; echo '<input type="radio" name = "nuts" value="nuts" id="nuts"> ' . $question['choice 1'] . '<br />'; echo '<input type="radio" name = "condensed milk" value="condensed milk" id="condensed milk"> ' . $question['choice 2'] . '<br />'; echo '<input type="radio" name = "semi-sweet morsels" value="semi-sweet morsels" id="semi-sweet morsels"> ' . $question['choice 3'] . '<br />'; echo '<input type="radio" name = "bakers chocolate" value="bakers chocolate" id="backers chocolate"> ' . $question['choice 4'] . '<br />'; } ?> </body> </html>

give related radio buttons same name. example:

foreach($questions $question){ echo $question['question'] . '<br />'; echo '<input type="radio" name = "nuts" value="nuts" id="nuts"> ' . $question['choice 1'] . '<br />'; echo '<input type="radio" name = "nuts" value="condensed milk" id="condensed_milk"> ' . $question['choice 2'] . '<br />'; echo '<input type="radio" name = "nuts" value="semi-sweet morsels" id="semi-sweet_morsels"> ' . $question['choice 3'] . '<br />'; echo '<input type="radio" name = "nuts" value="bakers chocolate" id="backers_chocolate"> ' . $question['choice 4'] . '<br />'; }

as farther note.. valid id attributes cannot contain spaces, in illustration above, have replaced spaces underscores.

php radio-button

No comments:

Post a Comment