Building a quiz script, but got some errors

Discussion in 'PHP' started by pornorexia, Dec 7, 2010.

  1. #1
    Heey

    I am builing some kind of quiz script.
    I got 10 images people see and they have to say if they agree or not.
    Simply by clicking yes / no. First i used a submit button, but now i want it to submit whem people click yes/no.
    tried some stuff already but cant find the correct way.

    Please help

    here is the code:

    <?php
    
    session_start();
    
    $thispage = 'test.php';
    $img = array( '1','2','3','4','5','6','7','8','9','10' );
    
    function getQuestion( $q ) {
        global $img;
        $curImg = $img[$q];
        return "Do you agree? <br> <img src=\"images/{$curImg}.jpg\">";
    }
    
     
    if ( isset( $_GET['reset'] ) ) {
        unset( $_SESSION['q'] );
        unset( $_SESSION['answer'] );
        header("Location: {$thispage}");
    }
    
    if ( !isset( $_SESSION['q'] ) ) {
        $_SESSION['q'] = 0;
    }
    if ( !isset( $_SESSION['answer'] ) ) {
        $_SESSION['answer'] = array();
    }
    
    
    if ( isset( $_POST['answer'] ) ) {
        $answer = $_POST['submit'];
        if ( $answer == '1' || $answer == '2' ) {
            $next =  $_SESSION['q'] + 1 ;
            if ( array_key_exists( $next,$img ) ) {
                $_SESSION['answer'][$_SESSION['q']] = $answer;
            }
            $_SESSION['q'] = $next ;
        }
    }
    
    if ( $_SESSION['q'] >= count( $img ) ) {
        $html = '';
        foreach( $_SESSION['answer'] as $q => $value ) {
            $html .= getQuestion( $q ) . " Answer: {$value}<br />";
        }
        $html .= "<br /><br /><a href=\"{$thispage}?reset=Y\">Reset Quiz</a>";
    }
    else {
        $question = getQuestion( $_SESSION['q'] );
        $html =<<<HTML
    
    <html>
    <body>
    <h1>{$question}</h1>
        <input type="submit" value="Yes" name="answer"  />Yes<br />
        <input type="submit" value="No" name="answer"  />No<br />
    </form>
    </body>
    </html>
    HTML;
    }
    
    echo $html;
    
    
    ?>
    Code (markup):
     
    pornorexia, Dec 7, 2010 IP
  2. mweldan

    mweldan Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have to put it in <form>

    
    else {
        $question = getQuestion( $_SESSION['q'] );
        $html =<<<HTML
    
    <html>
    <body>
    <h1>{$question}</h1>
        <form method="post">
        <input type="submit" value="Yes" name="answer"  />Yes<br />
        <input type="submit" value="No" name="answer"  />No<br />
        </form>
    </form>
    </body>
    </html>
    HTML;
    }
    
    Code (markup):
     
    mweldan, Dec 7, 2010 IP
  3. pornorexia

    pornorexia Peon

    Messages:
    39
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you,

    how stupid :D:)

    now got it working
     
    pornorexia, Dec 7, 2010 IP