Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Form Question

  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post

    Form Question

    Iam working on a MCQ script in php but having an error while everything looks fine to me.But when i leave the forms blank and submit i get an error.
    here is the code
    PHP Code:
    <?php 
    // Question 1 //
     
    $question1 $_GET['q1a'];
    if(
    $question1=='a')
    {
    echo 
    "Your answer to question 1 was correct!";
    }
    if(
    is_null($question1))
    {
    echo 
    "You didn't answer question 1.";
    }
    elseif(
    $question1 != 'a')
    {
    echo 
    "Your answer to question 1 was incorrect.";
    }
     
    // Question 2 //
     
    $question2 $_GET['q2a'];
     
    if(
    $question2=='a1')
    {
    echo 
    "Your answer to question 2 was correct!";
    }
    if(
    is_null($question2))
    {
    echo 
    "You didn't answer question 2.";
    }
    elseif(
    $question2 != 'a1')
    {
    echo 
    "Your answer to question 2 was incorrect.";
    }
     
     
    ?>
    Code:
    <body>
    
    
    
    <form method="GET" action="go.php">
    1 - Which is the most visited Website in the year 2010 according to a survey?<br>
    <input type=radio name='q1a' value='a'>Google<br>
    <input type=radio name='q1a' value='b'>Facebook<br>
    <input type=radio name='q1a' value='c'>Amazon<br>
    <input type=radio name='q1a' value='d'>Yahoo<br>
    <br>
    <br>
    <br>
    2 - What does PHP stand for ?<br>
    <input type=radio name='q2a' value='a1'> Private Home Page<br>
    <input type=radio name='q2a' value='b1'>PHP: Hypertext Preprocessor<br>
    <input type=radio name='q2a' value='c1'>Personal Hypertext Processor<br>
    <input type=radio name='q2a' value='d1'>Personal Home Page<br>
    <input type=submit value='Go'>
    
    
    
    </form>
    </body>
    </html>
    And the error i get is this
    ( ! ) Notice: Undefined index: q1a in C:\webserver\www\test\go.php on line 40
    Call Stack
    # Time Memory Function Location
    1 0.0351 367440 {main}( ) ..\go.php:0
    You didn't answer question 1.

    ( ! ) Notice: Undefined index: q2a in C:\webserver\www\test\go.php on line 56
    Call Stack
    # Time Memory Function Location
    1 0.0351 367440 {main}( ) ..\go.php:0
    You didn't answer question 2.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You need to check if the user actually answered the question:

    PHP Code:
    if (isset($_GET['q1a'])) {
        
    // they answered the question

    BTW The answer to question 2 is either b1 or d1. But b1 is perhaps more correct than d1
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post
    It is still not working getting the error again.For checking that user have answered or not i used.
    if(is_null($question2))
    Lol the answer to question 2 is b i just copy pasted the first one.

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    Quote Originally Posted by haseeb View Post
    It is still not working getting the error again.For checking that user have answered or not i used.
    if(is_null($question2))
    Heed the advice AndrewGSW gave you. You need to test whether or not a question was answered before you try to retrieve the value.

    PHP Code:
    $question1 '';
    if (isset(
    $_GET['q1a'])) {
        
    $question1 $_GET['q1a'];
    }
    if(
    $question1=='a') {
    echo 
    "Your answer to question 1 was correct!";
    } elseif (
    $question1 != 'a') {
    echo 
    "Your answer to question 1 was incorrect.";
    } else {
    echo 
    "You didn't answer question 1."

    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    haseeb (08-18-2012)

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Lol the answer to question 2 is b i just copy pasted the first one.
    So if I answered d would I receive no marks?
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    This code could be 'smartened up' in several ways. One would be to use javascript to see if any of the entries were made. Another would be to make a fifth default entry that would automatically be chosen or checked, something like "No choice made.'. Neither radio or checkbox entries return anything to the server if they aren't checked. Another way might be to use select boxes, a little more complex but foolproof.

  • #7
    New Coder
    Join Date
    Apr 2012
    Posts
    19
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks for the help and andrew and djm0219 its working now i simplified it more. Iam new to php iam just learning php by creating little simple scripts.
    PHP Code:
    // Question 1 //

    $question1 ''
    if (isset(
    $_GET['q1a'])) { 
        
    $question1 $_GET['q1a']; 


    if(
    $question1=='a')
    {
    echo 
    "Your answer to question 1 was correct!<br>";
    }
    if (
    trim($question1) == '') { 
       echo 
    'Please choose answer'

    elseif(
    $question1 != 'a')
    {
    echo 
    "Your answer to question 1 was incorrect.<br>";
    }
    // Question 2//
    $question2 ''
    if (isset(
    $_GET['q2a'])) { 
        
    $question2$_GET['q2a']; 


    if(
    $question2=='b')
    {
    echo 
    "Your answer to question 1 was correct!<br>";
    }
    if (
    trim($question1) == '') { 
       echo 
    'Please choose answer'

    elseif(
    $question1 != 'b')
    {
    echo 
    "Your answer to question 1 was incorrect.<br>";



  •  

    Posting Permissions

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