Jump to content

[SOLVED] Radio Button Form Help!


gigantorTRON

Recommended Posts

I'm creating a quiz type application that uses PHP to automatically load questions and their possible answers into an HTML page. The function I have that does this looks like this:

 

switch($type) {
		//single line text box
		case "T":

		while ($out = mysql_fetch_row($result)) {

		$output .= $out[0] . ": <input type='text' name='" . $qid . $x . "'><br>";
		$x++;
		}

		$x=0;

		break;

		//text area
		case "B":

		$output = "<form method='POST'>";

		while ($out = mysql_fetch_row($result)) {

		$output .= $out[0] . ": <textarea name='firstname" . $x . "' cols=40 rows=6></textarea><br>";
		$x++;
		}

		$x=0;

		break;

		//radio button
		case "R":

		while ($out = mysql_fetch_row($result)) {

		$output .= "<p><input type='radio' name='" . $qid . $x . "' value='" . $out[0] . "'>" . " " . $out[0] . "</p>";
		$x++;
		}

		$x=0;

		break;

		case "C":

		while ($out = mysql_fetch_row($result)) {

		$output .= "<p><input type='checkbox' name='" . $qid . $x . "' value='" . $out[0] . "'>" . " " . $out[0] . "</p>";
		$x++;
		}

		$x=0;

		break;

		//select (drop down) box

		case "S":

		$output = "<form method='POST'>";

		while ($out = mysql_fetch_row($result)) {

		$output .= $out[0] . ": <input type='select' name='firstname" . $x . "'><br>";
		$x++;
		}

		$x=0;

		break;

		default :
	}

		return $output;
}

 

This outputs the form elements with their names equal to question#choice i.e. 13a, 13b, etc. Another function then takes this data and saves it to a database table to track progress.

 

Here's the problem I'm encountering...

The html radio buttons are defined as a group by their 'name=' tag. Since each name tag is unique they are separate groups and a user could click multiple radio buttons rather than just one.

 

Is there a way around this via css or something else? Or maybe a better way to approach this task?

 

Thanks!

Link to comment
Share on other sites

Hey there,

i've run into this same issue myself.

 

the way i worked around it, was to create a hierarchy of data and to dig down into as needed.

in my db, here's how i set it up::

 

 

SurveyAnswerTypes
id, name ( radio, textbox, textfield, checkbox )

SurveyAnswers
id, survey_question_id, survey_answer_type_id, name, title, created, modified

SurveyQuestions
id, sort_ord, name, survey_id, created, modified

SurveyResponses
survey_id, survey_question_id, answer_value, ip, created

Surveys:
id, sort_ord, name, copy, status

 

so then in the application logic,

i loop over all the questions for this survey

each question can have an answer, that answer would be one of the survey types

the name of the answer (your radio, text, ....) would be the question_id

so you can have more then one answer type for each question (radios and select boxes)

 

ex of final result: http://www.soyconnection.com/newsletters/soy-connection/health-nutrition/survey.php?id=5

 

 

hope that helps a bit.

 

good luck

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.