p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Radio buttons and arrays


Message #1 by "Dave Sherer" <ToeShot@H...> on Sun, 8 Sep 2002 21:18:16
Does anyone know how to store a selected value of a radio button into an array after it is selected? I
know I am close I just can't quite grab it.



What I have is a QuestionID for the number of the question, that is the name of the radio button which I use for the indexing of my
array. What I need to do now is grab the value of the radio button that was selected. Now all my values are hard coded 1-5 but there
are times that a question may only have 2 or 3 or 4 answers. which I filter through if statements in a while statement. In which
everything works I just need to grab the selected value for the questionid at hand. 



TIA
Message #2 by "jorge" <jorge@d...> on Sun, 8 Sep 2002 18:01:36 -0500
Hi Dave:

take in consideration that all of the variables are already passed into the
$ _POST array  you can call them from there
A good reason to declare an array is if the form is dinamically generated if
not is not necesary.
since its already into de $_POST an a clean well organized naming system
will be easy an fast to handle.
place the next line in your script
var_dump($_POST);
this way you could see how the varas where passed an helps a lot when
debuging.
that way you can review what is going on.
Lets suppose you have named you radiobuttons like this
<input type="radio" name="answer_1" value="Value_1">
<input type="radio" name="answer_2" value="Value_2">

This way you can access any var passed an just treat in a case the vars that
need an special case the rest go to the deafault case simplyfing
an making real easy to go back to your script in the future.furthermore if
you need to insert into multiple tables you can create the arrays needed to
do so
an then INSERT all the info into several tables running the same loop for
each array to load; considering that you have an array of the tables to
which you are going to load the data.

foreach($_POST as $key => $values)
    {
    switch($key)
        {
        case "answer_1 ":
            $grabthis=$value;
        break;
        case "answer_2 ":
            $table_column_name[]=$key;
            $value_to_ insert_intable2[]=$value;
        break;
        case "answer_3 ":
            $table_column_name[]=$key;
            $value_to_ insert_intable2[]=$value;
        break;
        case "answer_4 ":
            $table_column_name[]=$key;
            $value_to_ insert_intable1[]=$value;
        break;
        default:
            $table_column_name[]=$key;
            $value_to_ insert_intable3[]=$value
        }
    }

to declare an array in the form just add [] after the $name[]
<input type="radio" name="answer[]" value="Value_1">
<input type="radio" name="answer[]" value="Value_2">
to access this answer array you could do it as above or by numeric index
building a for loop $answer[$i]


Jorge

:)



----- Original Message -----
Fro
m: "Dave Sherer" <ToeShot@H...>
To: "beginning php" <beginning_php@p...>
Sent: Sunday, September 08, 2002 9:18 PM
Subject: [beginning_php] Radio buttons and arrays


> Does anyone know how to store a selected value of a radio button into an
array after it is selected? I know I am close I just can't quite grab it.
>
>
>
> What I have is a QuestionID for the number of the question, that is the
name of the radio button which I use for the indexing of my array. What I
need to do now is grab the value of the radio button that was selected. Now
all my values are hard coded 1-5 but there are times that a question may
only have 2 or 3 or 4 answers. which I filter through if statements in a
while statement. In which everything works I just need to grab the selected
value for the questionid at hand.
>
>
>
> TIA


Message #3 by "Nikolai Devereaux" <yomama@u...> on Mon, 9 Sep 2002 12:50:47 -0700
Okay, you've got a bunch of QuestionID's, each uniquely identifies a
question and, therefore, a set of answers.  Each answer has it's own answer
ID which is unique among answers for a particular question, but not
necessarily unique among all answers.

The end result of this is that a combination of a question ID and an answer
ID uniquely identifies one answer.


For example:

Question 1:  How old are you?
A)  0-9
B) 10-13
C) 14-17
D) 18-22
E) 22-25
F) 26-35
G) 36+


Question 2: What is your gender?
A) Female
B) Male


A is an identifier which is not unique among all answers, but 1A uniquely
identifies "0-9".

Anyway, each question has a radio button for each possible answer.  Each
radio button in a set of related answers must have the same name -- that is
how the browser will make sure that only one of the options are selected.

Following this template should work:

<INPUT name="answer_x" value="y">z</INPUT>

where x is the question ID, y is the value ID, and z is the value text.


For example:

$questions = array("How old are you?",
                   "What is your gender?");

$answers = array(array("0-9", "10-13", "14-17"...),
                 array("Female", "Male"));


foreach($questions as $qid => $qtxt)
{
   echo "Question {$qid}: {$qtxt}\n";

   foreach($answers[$qid] as $ansid => $anstxt)
   {
      echo " <INPUT name  = \"answer_{$qid}\"
                    value = \"{$ansid}\">{$anstxt}</INPUT>\n";
   }
}



hope this makes sense to you!  Use the print_r() function to help you
understand the submitted form values.


if(isset($_POST))
{
    echo "<PRE>_POST is:\n";
    print_r($_POST);
    echo "</PRE>\n";
}



take care,

nik

Message #4 by "Dave Sherer" <ToeShot@H...> on Tue, 10 Sep 2002 05:24:47
Nik & Jorge
  I appreciate your responses but I may not have been clear enough or I don't understand your answers. Hopefully by posting my code
you will have a better view of what I am trying to do and why.

Below is my code, I am pulling questions and answers from a MySQL table. I am having one question per page, which is called from

($PHP_SELF?action=list_records&cur_page=$next_page)
or
($PHP_SELF?action=list_records&cur_page=$prev_page) 

depending wether they click either previous or next page.

When they select a radio button from below I need to store the questionID and it's value into the array. Once I get the values in
there if they select the previous page I would like to be able to pull from the array thier answer and select it for them also. Is
that possible?

Thank You For your Help guys.

while($query_data = mysql_fetch_array($result)) {
      $questionID = $query_data["questionID"];
      $question = $query_data["question"];
      $answ1 = $query_data["answ1"];
      $answ2 = $query_data["answ2"];
      $answ3 = $query_data["answ3"];
      $answ4 = $query_data["answ4"];
      $answ5 = $query_data["answ5"];

      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\" ALIGN=\"LEFT\">$question</TD><BR><BR>\n";
      echo "</TR>\n";

      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\" VALUE=\"1\">   $answ1</TD><BR>\n";
      echo "</TR>\n";

      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\" VALUE=\"2\">   $answ2</TD><BR>\n";
      echo "</TR>\n";

      echo "<TR>\n";
      if($answ3 != NULL){
      echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\" VALUE=\"3\">   $answ3</TD><BR>\n";
      }
      echo "</TR>\n";

      echo "<TR>\n";
      if($answ4 != NULL){
      echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\" VALUE=\"4\">   $answ4</TD><BR>\n";
      }
      echo "</TR>\n";

      echo "<TR>\n";
      if($answ5 != NULL){
      echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\" VALUE=\"5\">   $answ5</TD><BR>\n";
      }
      echo "</TR>\n";



Message #5 by "Nikolai Devereaux" <yomama@u...> on Mon, 9 Sep 2002 21:29:55 -0700
Okay, we're crossing streams, I guess...

>   I appreciate your responses but I may not have been clear
> enough or I don't understand your answers. Hopefully by posting
> my code you will have a better view of what I am trying to do and why.

Okay, I guess I would've set things up a bit differently in the database.  I
don't know what your schema is like, so I'll wing it with what I can gather
from your code.

I'm just going to post my suggestion and hope that it makes enough sense!


while($query_data = mysql_fetch_array($result))
{
   $questionID = $query_data["questionID"];
   $question = $query_data["question"];
   $answers[1] = $query_data["answ1"];
   $answers[2] = $query_data["answ2"];
   $answers[3] = $query_data["answ3"];
   $answers[4] = $query_data["answ4"];
   $answers[5] = $query_data["answ5"];

   echo "<TR>\n";
   echo "  <TD WIDTH=\"100%\" ALIGN=\"LEFT\">$question</TD><BR><BR>\n";
   echo "</TR>\n";

   foreach($answers as $ans_id => $answer)
   {
      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\">
            <INPUT TYPE=\"Radio\"
                  NAME=\"$questionID\"
                  VALUE=\"$ans_id\">$answer</TD><BR>\n";
      echo "</TR>\n";
   }

}

echo "</TR>\n";



Does this clear things up?


As far as preselecting a user's submitted answer, you'll either need to
store the user's submitted answers in the database or in a session variable.
Let's assume that you're storing all the user's answers in an array called
$USER_ANSWERS.  (read my TIP on organizing data inside arrays for why I
choose this)

The array is set up like this:

$USER_ANSWERS[question id] = answer id

Now, let's go back to the foreach() loop that generates all the radio INPUT
tags:


   foreach($answers as $ans_id => $answer)
   {
      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\">
            <INPUT TYPE=\"Radio\"
                  NAME=\"$questionID\"
                  VALUE=\"$ans_id\"";

      if( (isset($USER_ANSWERS[$questionID]) &&
          ($USER_ANSWERS[$questionID] == $ans_id) )
      {
         echo " CHECKED"
      }

      echo ">$answer</TD><BR>\n";
      echo "</TR>\n";
   }



See what I'm doing?  I'm just checking the answer that the user's submitted,
if any.


hope this helps,

nik

Message #6 by "jorge" <jorge@d...> on Tue, 10 Sep 2002 10:50:29 -0500
Dave:
this part of your code is confusing me
while($query_data = mysql_fetch_array($result)) {
>       $questionID = $query_data["questionID"];
>       $question = $query_data["question"];
>       $answ1 = $query_data["answ1"];
>       $answ2 = $query_data["answ2"];
>       $answ3 = $query_data["answ3"];
>       $answ4 = $query_data["answ4"];
>       $answ5 = $query_data["answ5"];
this means that you are storing every answer for each question (question ID,
question itself and 5 possible answers for this question); but it seems that
this table is already filled with your info.
then you have mentioned to store user answers WHERE?? i guess you will store
user name or data at least right?
in that case you need a table to do so
like table_users (user_name,question1_answer,question_2answer, etc)
 please let me know if iam in the right direction

Jorge




----- Original Message -----
From: "Dave Sherer" <ToeShot@H...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, September 10, 2002 5:24 AM
Subject: [beginning_php] RE: Radio buttons and arrays


> Nik & Jorge
>   I appreciate your responses but I may not have been clear enough or I
don't understand your answers. Hopefully by posting my code you will have a
better view of what I am trying to do and why.
>
> Below is my code, I am pulling questions and answers from a MySQL table. I
am having one question per page, which is called from
>
> ($PHP_SELF?action=list_records&cur_page=$next_page)
> or
> ($PHP_SELF?action=list_records&cur_page=$prev_page)
>
> depending wether they click either previous or next page.
>
> When they select a radio button from below I need to store the questionID
and it's value into the array. Once I get the values in there if they select
the previous page I would like to be able to pull from the array thier
answer and select it for them also. Is that possible?
>
> Thank You For your Help guys.
>
> while($query_data = mysql_fetch_array($result)) {
>       $questionID = $query_data["questionID"];
>       $question = $query_data["question"];
>       $answ1 = $query_data["answ1"];
>       $answ2 = $query_data["answ2"];
>       $answ3 = $query_data["answ3"];
>       $answ4 = $query_data["answ4"];
>       $answ5 = $query_data["answ5"];
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\" ALIGN=\"LEFT\">$question</TD><BR><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"1\">   $answ1</TD><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"2\">   $answ2</TD><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ3 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"3\">   $answ3</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ4 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"4\">   $answ4</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ5 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"5\">   $answ5</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>
>


Message #7 by "Dave Sherer" <ToeShot@H...> on Thu, 12 Sep 2002 05:22:17
> 
Okay, we're crossing streams, I guess...

>   I appreciate your responses but I may not have been clear
> enough or I don't understand your answers. Hopefully by posting
> my code you will have a better view of what I am trying to do and why.

Okay, I guess I would've set things up a bit differently in the database.  I
don't know what your schema is like, so I'll wing it with what I can gather
from your code.

I'm just going to post my suggestion and hope that it makes enough sense!


while($query_data = mysql_fetch_array($result))
{
   $questionID = $query_data["questionID"];
   $question = $query_data["question"];
   $answers[1] = $query_data["answ1"];
   $answers[2] = $query_data["answ2"];
   $answers[3] = $query_data["answ3"];
   $answers[4] = $query_data["answ4"];
   $answers[5] = $query_data["answ5"];

   echo "<TR>\n";
   echo "  <TD WIDTH=\"100%\" ALIGN=\"LEFT\">$question</TD><BR><BR>\n";
   echo "</TR>\n";

   foreach($answers as $ans_id => $answer)
   {
      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\">
            <INPUT TYPE=\"Radio\"
                  NAME=\"$questionID\"
                  VALUE=\"$ans_id\">$answer</TD><BR>\n";
      echo "</TR>\n";
   }

}

echo "</TR>\n";



Does this clear things up?


As far as preselecting a user's submitted answer, you'll either need to
store the user's submitted answers in the database or in a session variable.
Let's assume that you're storing all the user's answers in an array called
$USER_ANSWERS.  (read my TIP on organizing data inside arrays for why I
choose this)

The array is set up like this:

$USER_ANSWERS[question id] = answer id

Now, let's go back to the foreach() loop that generates all the radio INPUT
tags:


   foreach($answers as $ans_id => $answer)
   {
      echo "<TR>\n";
      echo "<TD WIDTH=\"100%\">
            <INPUT TYPE=\"Radio\"
                  NAME=\"$questionID\"
                  VALUE=\"$ans_id\"";

      if( (isset($USER_ANSWERS[$questionID]) &&
          ($USER_ANSWERS[$questionID] == $ans_id) )
      {
         echo " CHECKED"
      }

      echo ">$answer</TD><BR>\n";
      echo "</TR>\n";
   }



See what I'm doing?  I'm just checking the answer that the user's submitted,
if any.


hope this helps,

nik



This is Great Thank you so Much :o)  Sorry took So long to reply. My monitor took flying lessons.
Message #8 by "Dave Sherer" <ToeShot@H...> on Thu, 12 Sep 2002 05:30:34
> Dave:
this part of your code is confusing me
while($query_data = mysql_fetch_array($result)) {
>       $questionID = $query_data["questionID"];
>       $question = $query_data["question"];
>       $answ1 = $query_data["answ1"];
>       $answ2 = $query_data["answ2"];
>       $answ3 = $query_data["answ3"];
>       $answ4 = $query_data["answ4"];
>       $answ5 = $query_data["answ5"];
this means that you are storing every answer for each question (question ID,
question itself and 5 possible answers for this question); but it seems that
this table is already filled with your info.
then you have mentioned to store user answers WHERE?? i guess you will store
user name or data at least right?
in that case you need a table to do so
like table_users (user_name,question1_answer,question_2answer, etc)
 please let me know if iam in the right direction

Jorge




----- Original Message -----
From: "Dave Sherer" <ToeShot@H...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, September 10, 2002 5:24 AM
Subject: [beginning_php] RE: Radio buttons and arrays


> Nik & Jorge
>   I appreciate your responses but I may not have been clear enough or I
don't understand your answers. Hopefully by posting my code you will have a
better view of what I am trying to do and why.
>
> Below is my code, I am pulling questions and answers from a MySQL table. I
am having one question per page, which is called from
>
> ($PHP_SELF?action=list_records&cur_page=$next_page)
> or
> ($PHP_SELF?action=list_records&cur_page=$prev_page)
>
> depending wether they click either previous or next page.
>
> When they select a radio button from below I need to store the questionID
and it's value into the array. Once I get the values in there if they select
the previous page I would like to be able to pull from the array thier
answer and select it for them also. Is that possible?
>
> Thank You For your Help guys.
>
> while($query_data = mysql_fetch_array($result)) {
>       $questionID = $query_data["questionID"];
>       $question = $query_data["question"];
>       $answ1 = $query_data["answ1"];
>       $answ2 = $query_data["answ2"];
>       $answ3 = $query_data["answ3"];
>       $answ4 = $query_data["answ4"];
>       $answ5 = $query_data["answ5"];
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\" ALIGN=\"LEFT\">$question</TD><BR><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"1\">   $answ1</TD><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"2\">   $answ2</TD><BR>\n";
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ3 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"3\">   $answ3</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ4 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"4\">   $answ4</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>       echo "<TR>\n";
>       if($answ5 != NULL){
>       echo "<TD WIDTH=\"100%\"><INPUT TYPE=\"Radio\" NAME=\"$questionID\"
VALUE=\"5\">   $answ5</TD><BR>\n";
>       }
>       echo "</TR>\n";
>
>
>




Yes jorge I am pulling the question and all the possible
Answers from the database. Then I want to store the answer
the user selected and the corect answer into an array to show them thier score.

Hope that makes sense

  Return to Index