Stop Submit button clearing field

Hi, I’m working on some php code (for multiple choice test questions) which is included with HTML, and I find that pressing the Submit button erases the answer that
was selected in the dropdown menu. Otherwise the code works correctly. HOw can I prevent the Submit button from erasing the answer filled in by the user?

THis is the code:



<form method="post">
 
<p>1. &nbsp;
Mary &nbsp; 
<SELECT name="question1">
<OPTION value=na>
<OPTION value=A>tell
<OPTION value=B> said
<OPTION value=C>told
<OPTION value=D>says

 </SELECT>&nbsp;

 
that she would be late.

 <?php 
    
    // QUESTION ONE
   
   
     
	if ($_POST["question1"]=="B") {
 echo '  <td style="height:80px; align : middle">&nbsp; 1. </td> <td style="green">Correct!  <br /> </td> </tr><tr>' ;   
 
 }
 elseif ($_POST["question1"]=="na")  {
   echo '<td>&nbsp; 1.</td><td> Please answer this question <br /></td> </tr><tr>';
 }

 
elseif ($_POST["question1"]!=="B")  {
    echo '<td>&nbsp; 1.</td><td style="red"> Mary  said  that she would be late. <br /></td> </tr><tr>';
 }
 ?>
<input type="submit" value="Submit" />
</form>
   

Thanks, any help appreciated

When the form refreshes, after being submitted, you have to add a selected=“selected” attribute to the selected option:

<SELECT name="question1">
<OPTION value=na>
<OPTION value=A selected="selected">tell
<OPTION value=B> said
<OPTION value=C>told
<OPTION value=D>says
 </SELECT>

and don’t forget the quotes around the values.

OK, great! I tried that, but still have an issue - maybe I’m doing something wrong?

I have changed the code to this:


Mary &nbsp; 
<SELECT name="question1">
 <OPTION value=na selected="selected">
 <OPTION value=A selected="selected">tell
 <OPTION value=B selected="selected">said
 <OPTION value=C selected="selected">told
 <OPTION value=D selected="selected">says

 </SELECT>&nbsp;

 
that she would be late.  

But then, after pressing Submit, I get:



  Mary [B]says [/B]   that she would be late.   1. Correct! 

The word “says” is incorrect - it should be “said” (Option=B…)

Only one option should be selected, the one the visitor picked. :wink:

Oh? But then how can I know which option to select? The user may select A,B,C or D…? Should the “selected” be placed somewhere in this line?

 <SELECT name="question1"> 

The answer is in the $_POST variable. Print it out at the start of your script:

print_r ($_POST);

Sorry, can you explain that a little more?

I don’t understand why you selected “A” here:


<SELECT name="question1">
<OPTION value=na>
<OPTION value=A selected="selected">tell
<OPTION value=B> said
<OPTION value=C>told
<OPTION value=D>says
 </SELECT> 

Thanks.

You have a form. When the visitor hits the Submit button the data from the form is sent to the server and made available to php in the $_POST variable. How were you planning on dealing with the form data?

The $_POST variable is an associative array and it’s best understood by looking at its contents with

echo "<pre>";
print_r $_POST;
echo "</pre>";

this will show you what the form sent to the server. You’ll get something like

Array
(
    [name] => My Name
    [company] => XEROX
    [telephone] => 123-3456
    [email] => 
    [comments] => MORE INFO
)

where the key is the input field name and the value is the value entered in the form.

Ok, I will try to assimilate that information. What I’m basically trying to do is: When a user does the online multiple choice test,
they answer the questions. Then, after pressing submit, the user can see the test results on the same (php) page. But I want them to be able to
repeat any incorrect answers - and press submit again. Basically, I see that this ability to re-do the questions is working - the only problem is that all the answers they gave are “erased” by clicking Submit, so they may forget which incorrect answer they selected the first time.

Actually, I think there may have been a misunderstanding. I don’t want the users’ test answers sent to me. I just wanted the user to be still able to see the answers they selected, after pressing Submit. The rest (re-doing the incorrect answers) is working ok.

The function of the submit button is to send the form input to the server which serves up a new page without any input unless you put it there.

If you just want client side functionality you need javascript not php which is server side scripting.

I really don’t understand what you are up to. :confused: Can you explain more fully what you are doing?

Ok. I’m trying to make a multiple choice test with dropdown menus. So, the user sees the test, answers the questions, presses Submit which gives them the
results of the test - they would see the same page with new information (correct! incorrect etc), something like this:

  1. Mary [ ] that she would be late. Correct!

2 [ ] me about your new ideas Incorrect!

etc…

In between the [ ] represents the dropdown menu, which becomes blank on pressing Submit. I would like the user to still be able to see the answer they gave
in the dropdown menu. That’s basically all. The other aspects work ok, like redoing the incorrect answers.

So you do have a script that scores the test. That script must refresh the drop down menus as suggested at the start of this thread. It “knows” which options were selected.

Just that recently I saw an online test which was as I would like: The user answers the questions, presses “Check Answers” or whatever, and some nice green “correct” ticks appear after the correct answers, and the incorrect ones have red “X’s” and can be re-answered there and then. Seemed really convenient for the user. I don’t know if that test was php though. It’s on a password protected school site that I’m not a member of. I was hoping to emulate that with php, by having all the HTML test questions and php code on one .php page. But maybe it’s not possible?

I’ve told you how to do it with php already! You examine $_POST and respond accordingly. Until you start writing some code I can’t help you further.

The thing is I’m not very proficient in php, so I don’t quite grasp the answers. Thanks anyway for your efforts.

Try googling “basic php tutorial”

In the test there are 10 questions, but I think there was a misunderstanding because this isn’t the way I wanted
to have the information displayed:

Array
(
[question1] => A
[question2] => B
[question3] => A
[question4] => B
[question5] => C
[question6] => A
[question7] => C
[question8] => B
[question9] => A
[question10] => C
)
I mean, not in this format

My idea was that the user would be able to see the complete questions with the answers they had selected, and marked correct or incorrect,

and be able to just select a different answer from the same dropdown boxes, replacing the incorrect answers. I was able to see the above feedback by inserting this
script at the beginning of the PHP


<?php 
 

  echo "<pre>";
  print_r($_POST);
  echo "</pre>";

/*
    Array
    (
        
    )
*/


And


<OPTION value="A" selected="selected">tell
 <OPTION value="B" >said
<OPTION value="C" >told
<OPTION value="D">says

Oh, it also works without the selected=“selected” in OPTION value=“A” in the above post.
and without the


/*
    Array
    (
        
    )
*/

Obvious, I suppose with the /* …*/

Hi,

As Denny said in post#11, PHP might be the wrong tool for the job in this case.
Luckily, I’ve recently been playing about with promised-based form validation and a mini-framework which was developed for this purpose.

Using this, I’ve made a small demo doing what you describe.
Currently it only marks an answer if it is incorrect, as well as giving you feedback once all of the answers are correct.
It would however be very easy to extend this so that correct answer are highlighted as well.

The beauty of this method is that you can create as many questions as you want and then pass in the correct answers as an object literal.

var validationConfig = {
  '.question_1': { answer: 'a' },
  '.question_2': { answer: 'c' }
  etc ...
};

That’s all you have to do.

The obvious downside to this is that all of the answers are available in the source code.
Also, if you want to do anything with the data other than let your users have some fun, you cannot ignore server-side validation.

If you are interested in this, then let me know and I can flesh out the demo for you a little and post/send you all of the code.