SitePoint Sponsor

User Tag List

Results 1 to 25 of 38

Hybrid View

  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


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
  •