SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 38 of 38
  1. #26
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by captainccs View Post
    It's:

    <?php your php code here ?>
    Indeed. A typo

  2. #27
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It would be a good idea of you posted the code for the whole form, not just a snippet.
    Denny Schlesinger
    web services

  3. #28
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by captainccs View Post
    It would be a good idea of you posted the code for the whole form, not just a snippet.
    Sure!

    So you have a basic form:

    index.php:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My quiz</title>
      </head>
      
      <body>
        <form action="" method="post">
          <p>Where does the Queen live?</p>
          <select name="question1">
            <option>Choose answer</option>
            <option value="a">New York</option>
            <option value="b">London</option>
            <option value="c">Dehli</option>
            <option value="d">My House</option>
          </select>
          
          <p><input type="submit"></p>
        </form>
      </body>
    </html>
    This form, when submitted, will post the form data back to itself.
    However, when you do this, the page will be reloaded and the form elements (the select in this case) will be reset to their original values.

    What we need to do to avoid this, is to catch the form data before displaying the form and then set the selected value of the select element accordingly.
    The only caveat is that we don't want to do this when the form first renders, as the user will have input no data.
    We can get around this by adding a hidden input field.

    PHP Code:
    <?php
    if (isset($_POST['submitted'])){
        
        
    // The form has been submitted
        // We can do stuff here
    }
    ?>

    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My quiz</title>
      </head>
      
      <body>
        <form action="" method="post">
          <p>Where does the Queen live?</p>
          <select name="question1">
            <option>Choose answer</option>
            <option value="a">New York</option>
            <option value="b">London</option>
            <option value="c">Dehli</option>
            <option value="d">My House</option>
          </select>
          <input type="hidden" name="submitted" value="1">

          <p><input type="submit"></p>
        </form>
      </body>
    </html>
    Now we are in a position to retrieve whatever the user submitted from the $_POST array.

    PHP Code:
    $answer1 $_POST['question1']; 
    We can then use that value to echo the selected attribute accordingly:

    Code PHP:
    <option value="a" <?php if($answer1=='a'){echo 'selected';} ?>>New York</option>   
    <option value="b" <?php if($answer1=='b'){echo 'selected';} ?>>London</option> 
    <option value="c" <?php if($answer1=='c'){echo 'selected';} ?>>Dehli</option> 
    <option value="d" <?php if($answer1=='d'){echo 'selected';} ?>>My House</option>

    We will also have to declare the variable $answer1, otherwise this will throw an error when the formfirst renders.

    Here's the complete code:

    PHP Code:
    <?php
    $answer1 
    '';

    if (isset(
    $_POST['submitted'])){
      
    $answer1 $_POST['question1'];
      echo 
    "<p>You chose answer " $answer1 "</p>";
    }
    ?>

    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My quiz</title>
      </head>
      
      <body>
        <form action="" method="post">
          <p>Where does the Queen live?</p>
          <select name="question1">
            <option>Choose answer</option>
            <option value="a" <?php if($answer1=='a'){echo 'selected';} ?>>New York</option>   
            <option value="b" <?php if($answer1=='b'){echo 'selected';} ?>>London</option> 
            <option value="c" <?php if($answer1=='c'){echo 'selected';} ?>>Dehli</option> 
            <option value="d" <?php if($answer1=='d'){echo 'selected';} ?>>My House</option> 
          </select>
          <input type="hidden" name="submitted" value="1">

          <p><input type="submit"></p>
        </form>
      </body>
    </html>
    I hope that helps you somewhat.
    By way of a disclaimer, I am no PHP master, so this is only meant to demonstrate a principle.
    It should work ok on a single page with a few questions, but as soon as your site grows, it would be better to adopt a more maintainable approach (such as using controllers and views).

    Also, now that we have some code to work with, Denny might have some suggestions for improvement.

  4. #29
    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 Pullo View Post

    What we need to do to avoid this, is to catch the form data before displaying the form and then set the selected value of the select element accordingly.
    The only caveat is that we don't want to do this when the form first renders, as the user will have input no data.
    We can get around this by adding a hidden input field.

    PHP Code:
    <?php
    if (isset($_POST['submitted'])){
        
        
    // The form has been submitted
        // We can do stuff here
    }
    ?>
    You don't need the hidden field. Use the $_SERVER variable instead. On first entry,

    $_SERVER['REQUEST_METHOD'] == "GET"

    on entry using the Submit button

    $_SERVER['REQUEST_METHOD'] == "POST"
    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == "POST"){
        
        
    // The form has been submitted
        // We can do stuff here
    }
    ?>
    Last edited by captainccs; May 7, 2013 at 14:08. Reason: Added missing quotes
    Denny Schlesinger
    web services

  5. #30
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Good tip. I didn't know you could do that.
    Thanks

  6. #31
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    You don't need the hidden field. Use the $_SERVER variable instead. On first entry,

    $_SERVER['REQUEST_METHOD'] == "GET"

    on entry using the Submit button

    $_SERVER['REQUEST_METHOD'] == "POST"
    PHP Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] == "POST"){
        
        
    // The form has been submitted
        // We can do stuff here
    }
    ?>
    Thanks for all that! I'll let you know how it goes!

  7. #32
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again, sorry I've been a bit focused on other things lately...

    I tried the suggested code but I can't get it to do exactly what I want. When I select the correct answer 'B' in the below code,

    I get an "ECHO" that corresponds to
    Code:
     elseif  ($_POST['question1'] !== "B")
    This is the complete code
    Code:
      <?php
    $answer1 = '';
    
    if ($_SERVER['REQUEST_METHOD'] == "POST"){
      $answer1 = $_POST['question1'];
    
     } 
    ?>
    
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My quiz</title>
      </head>
      
      <body>
      
    <form method="post">
     
    <p>1. &nbsp;
    Mary &nbsp; 
    <SELECT name="question1">
    <OPTION value=na>
            <option value="a" <?php if($answer1=='a'){echo 'selected';} ?>>tell</option>   
            <option value="b" <?php if($answer1=='b'){echo 'selected';} ?>>said</option> 
            <option value="c" <?php if($answer1=='c'){echo 'selected';} ?>>told</option> 
            <option value="d" <?php if($answer1=='d'){echo 'selected';} ?>>says</option> 
    
     </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>
      </body>
    </html> 
    
      </body>
    </html>
    Thanks for any help.

  8. #33
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Instead of !== try !=

    Does that help?

  9. #34
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried that ! = "B" , same result. Actually I think both forms are acceptable (! == and ! =) ?

    When I try the code as a "user", and select the various answers, I get the following echoed results:

    "na" "please answer this question"
    "A" "Mary said she would be late"
    "B" "Mary said she would be late"
    "C" "Mary said she would be late"
    "D" "Mary said she would be late"

    The selected answer does stay in in the dropdown box after submitting.

    Basically everything works, except that selecting the correct answer "B" doesn't echo out the "correct"

  10. #35
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    The option values are all lower case. Try changing (for example) != "B" into != "b"
    Apart from that, I'm away from the PC at the moment, so let me know if this doesn't work and I will have a look later.

  11. #36
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    spain
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant! The solution was to change to lower case, from != "B", to !="b".

    Now, finally, on with adding some nice css styles..

    You both have really helped me! Thanks a million!

  12. #37
    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
    Brilliant! The solution was to change to lower case, from != "B", to !="b".

    Now, finally, on with adding some nice css styles..

    You both have really helped me! Thanks a million!
    Right, "B" is not equal to "b"! Glad it works!
    Denny Schlesinger
    web services

  13. #38
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    No probs
    Glad it works


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
  •