Jump to content

Building a quiz script, but got some errors


freekg

Recommended Posts

He,

 

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;


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.