SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 38
  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    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

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When the form refreshes, after being submitted, you have to add a selected="selected" attribute to the selected option:
    HTML Code:
    <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.
    Denny Schlesinger
    web services

  3. #3
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, great! I tried that, but still have an issue - maybe I'm doing something wrong?

    I have changed the code to this:

    Code:
    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:
    Code:
      Mary says    that she would be late.   1. Correct!
    The word "says" is incorrect - it should be "said" (Option=B...)

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Only one option should be selected, the one the visitor picked.
    Denny Schlesinger
    web services

  5. #5
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    Code:
     <SELECT name="question1">

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dubman View Post
    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?

    Code:
     <SELECT name="question1">
    The answer is in the $_POST variable. Print it out at the start of your script:
    PHP Code:
    print_r ($_POST); 
    Denny Schlesinger
    web services

  7. #7
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, can you explain that a little more?

    I don't understand why you selected "A" here:
    Code:
    <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.

  8. #8
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dubman View Post
    Soor, can you expain that a little more? 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
    PHP Code:
    echo "<pre>";
    print_r $_POST;
    echo 
    "</pre>"
    this will show you what the form sent to the server. You'll get something like
    Code:
    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.
    Denny Schlesinger
    web services

  9. #9
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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. Can you explain more fully what you are doing?
    Denny Schlesinger
    web services

  12. #12
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    Denny Schlesinger
    web services

  14. #14
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  15. #15
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dubman View Post
    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.
    Denny Schlesinger
    web services

  16. #16
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is I'm not very proficient in php, so I don't quite grasp the answers. Thanks anyway for your efforts.

  17. #17
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try googling "basic php tutorial"
    Denny Schlesinger
    web services

  18. #18
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <?php 
     
    
      echo "<pre>";
      print_r($_POST);
      echo "</pre>";
    
    /*
        Array
        (
            
        )
    */
    And
    Code:
    <OPTION value="A" selected="selected">tell
     <OPTION value="B" >said
    <OPTION value="C" >told
    <OPTION value="D">says

  19. #19
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, it also works without the selected="selected" in OPTION value="A" in the above post.
    and without the
    Code:
    /*
        Array
        (
            
        )
    */
    Obvious, I suppose with the /* .....*/

  20. #20
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,066
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by dubman View Post
    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.
    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.

    Code JavaScript:
    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.

  21. #21
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nah..... I wouldn't like the answers to be visible in the source code, because if all the answers became 'public', I'd probably want
    to change the test questions regularly, whereas now I can just keep using the same test. Not that many users would think of checking the souce
    code for answers, but anyway... Thanks for the suggestion.
    Actually I did get reasonably near doing what I wanted - after clicking on submit, the user would see the answers marked correct or incorrect, but the dropdown menus
    would all become blank (Submit, having "erased" (refreshed) the answers). I could always just type the "erased" answers, maybe just before the "correct" /"incorrect" ticks - not ideal - I would have preferred that the user, after clicking Submit, could see all the questions just like before starting the test, but now with the "correct" / "incorrect" ticks, and the drop down boxes still showing the original selected answers.
    I fact, I was happy with my own particular workaround (I think based on the code at the start of this thread) until I saw another site which was very impressive in the way the user could easily redo incorrect answers, without having to click "page back". I assume that site is possibly javascript. I might see if can find out when I see it again (the next time I see the person with the site password). Anyway, I assume the bottom line is that any javascript solution would reveal the answers in the source code - a good reason to prefer php ;o) But that javascript test that you suggest looks very good, and I'm sure would do the job perfectly, albeit with the answers available in source code...

  22. #22
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,066
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by dubman View Post
    Actually I did get reasonably near doing what I wanted - after clicking on submit, the user would see the answers marked correct or incorrect, but the dropdown menus
    would all become blank (Submit, having "erased" (refreshed) the answers). I could always just type the "erased" answers, maybe just before the "correct" /"incorrect" ticks - not ideal - I would have preferred that the user, after clicking Submit, could see all the questions just like before starting the test, but now with the "correct" / "incorrect" ticks, and the drop down boxes still showing the original selected answers.
    Denny told you how to do this already:
    With a pure PHP solution, you would retrieve the answers from the $_POST array, evaluate them and echo "selected" in the correct place.

    e.g. (pseudo code)

    PHP Code:
    $answer1 $_POST['question1'];

    ...

    <
    select>
      <
    option value="A" <php? if($answer1=='A'){echo 'selected'?>>Answer A</option>  
      <option value="B" <php? if($answer1=='B'){echo 'selected'} ?>>Answer B</option>
      <option value="C" <php? if($answer1=='C'){echo 'selected'} ?>>Answer C</option>
      <option value="D" <php? if($answer1=='D'){echo 'selected'} ?>>Answer D</option>
    </select> 
    Quote Originally Posted by dubman View Post
    Anyway, I assume the bottom line is that any javascript solution would reveal the answers in the source code - a good reason to prefer php ;o)
    Not necessarily. I was just trying to make my previous example simple.
    What I would then do, is this:
    Have the form fire off an AJAX request to a PHP script when it is submitted.
    In the PHP script you could evaluate the answers, then return an array corresponding to right/wrong for each question.
    Then with JS, iterate over the response and add a tick/cross correspondingly.
    The user doesn't see a page refresh, the page feels responsive and snappy and the answer's aren't visible anywhere.

    This is personally, the way I would go about this.
    I would then add a second-tier of server-side validation, so that those people without JavaScript can still use the site.

    HTH

  23. #23
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks for that. Actually, thinking again, it wouldn't be a big deal if the answers were visible in the source code since, in this case, it's just for making exercises as opposed to a test of the users' level. Nevertheless, i like the way php code is "invisible". I appreciate the help!

  24. #24
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you probably gather, I'm not a very proficient with php. I have been trying to implement
    that code you posted:
    Code:
    $answer1 = $_POST['question1']; 
    
    ... 
    
    <select> 
      <option value="A" <php? if($answer1=='A'){echo 'selected'} ?>>Answer A</option>   
      <option value="B" <php? if($answer1=='B'){echo 'selected'} ?>>Answer B</option> 
      <option value="C" <php? if($answer1=='C'){echo 'selected'} ?>>Answer C</option> 
      <option value="D" <php? if($answer1=='D'){echo 'selected'} ?>>Answer D</option> 
    </select>
    And unfortunately I can't get it to work in my project. Do you think you could help me to
    implement it? <php? (in the above code) looks strange to me. Should it not be <?php ?

    Or could you recommend a tutorial that would be specific to this kind of coding?

    Thanks

  25. #25
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's:

    <?php your php code here ?>
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •