Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

updating question???? help

Status
Not open for further replies.

cs7536

Programmer
Apr 20, 2003
130
US
I created an application to insert questions into a quiz, I then created a update/modify form so that I can update my questions rather than deleting them and recreating a new ones. This is so that I can keep the same ID's..

The correct andwers are entered into the DB by way of radio buttons..

question:
________________
| |
| |
| |
-----------------------

a. O
b. O
c. O
d. O none of the above

when I select a question for update, my update/modify script re-inserts the data back into the question textarea but it will not re-insert the selected radio option.


can someone render assistance on this one?

thanks

Nothing is hard when you realy want to learn it.

Max
 
Hi cs7536,

... something like this, perhaps?!

Code:
<?
  $index = $_REQUEST['index'];
  $answer = $_REQUEST['answer']; 

  $question[] = &quot;Is PHP the best Server Side Scripting Language ever?&quot;;
  $answer1[] = &quot;Yes&quot;;
  $answer2[] = &quot;No&quot;;
  $answer3[] = &quot;Sometimes&quot;;
  $answer4[] = &quot;None of the above&quot;;
  
  $question[] = &quot;Is this cool or what?&quot;;
  $answer1[] = &quot;Yes&quot;;
  $answer2[] = &quot;No&quot;;
  $answer3[] = &quot;Not really&quot;;
  $answer4[] = &quot;None of the above&quot;;

  function saveAnswer($questionNo, $answer) {
    echo &quot;You selected answer <B>$answer</B><BR>&quot;;
    echo &quot;... to question no. <B>$questionNo</B>&quot;;
    // Your DB handler goes here...
  }
?>

<HTML>
<HEAD>
  <TITLE>POP QUIZ</TITLE>
</HEAD>
<BODY BGCOLOR=&quot;#FFFFFF&quot; TEXT=&quot;#000000&quot; LINK=&quot;#0000FF&quot; VLINK=&quot;#800080&quot; ALINK=&quot;#FF0000&quot;>

<H1>POP QUIZ</H1>

<?
  if(!$index)
    $index = 0;
  else
    saveAnswer(($index-1),$answer);
?>

<BR><BR>
<FORM ACTION=&quot;<? echo $PHP_SELF; ?>&quot; METHOD=&quot;POST&quot;>
  <TABLE BORDER=&quot;0&quot; CELLPADDING=&quot;5&quot;>
    <TR>
      <TD>
        <B>Question</B>
      </TD>
      <TD>
        <B>Your answer</B>
      </TD>
    </TR>
    <TR>
      <TD VALIGN=&quot;TOP&quot;>
        <? echo $question[$index]; ?>
      </TD>
      <TD>
        <INPUT NAME=&quot;answer&quot; TYPE=&quot;RADIO&quot; VALUE=&quot;a&quot;> 
          <? echo $answer1[$index]; ?> <BR>
        <INPUT NAME=&quot;answer&quot; TYPE=&quot;RADIO&quot; VALUE=&quot;b&quot;>
          <? echo $answer2[$index]; ?> <BR>
        <INPUT NAME=&quot;answer&quot; TYPE=&quot;RADIO&quot; VALUE=&quot;c&quot;>
          <? echo $answer3[$index]; ?> <BR>
        <INPUT NAME=&quot;answer&quot; TYPE=&quot;RADIO&quot; VALUE=&quot;d&quot;>
          <? echo $answer4[$index]; ?> <BR>
      </TD>
    </TR>
  </TABLE>
  <INPUT TYPE=&quot;hidden&quot; NAME=&quot;index&quot; VALUE=&quot;<? $index++; echo $index; ?>&quot;>
  <INPUT TYPE=&quot;submit&quot; VALUE=&quot;Next Question ...&quot;>
</FORM>
</BODY>
</HTML>

Good luck with your project §;O)


Jakob
 
Thanks jacob I will try to follow your instructions.

I've been having problems with making the info pop back up inside the radio buttons, partial because the radio buttons all have the same name, when I insert the data initialy the seleted radio is inserted into a single column in a table in the DB. so there is not a, b, c or d in the DB..

there is only one column name: answer. so when trying to retreive back to radio button for update being that they all have the same name it is not clear to me as to how answer finds the correct radio button. In return being that it reconizes all the radios the same it does not choose a radio at all..

My radio script currently looks like this:

<input type='radio' name='answer' value='<?php echo &quot;answer&quot;, ?>

keep in mind that there is 4 duplicate lines of this.

The php is so that it will load the data back into the radio button.

Originally in the insert page value was value='a' and so on, but for updating value='a' was replaced with the PHP so that it would load the data there from the DB.

So how can it know to put it back there if all the radios share the same name, and how will it know what the new answer would be if I decided to change the answer? if the value is no longer a, b, c or d it is <?php


can you explain

Nothing is hard when you realy want to learn it.

Max
 
Max,

I'm not sure what you're asking. However:

Radio-inputs with same name allows users to select only one of the inputs.

In my example, [/code]$question[/code] is an array. Just add more questions like I did it. [/code]$answer1[/code] thru [/code]$answer4[/code] are arrays too. They each contain one option to select.

When the first question has been answred, the result is stored in [/code]$answer[/code], so if you like, you can add that to your database table coloumn.

Please elaborate a bit... §;O)


Jakob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top