freekg Posted December 7, 2010 Share Posted December 7, 2010 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/220968-building-a-quiz-script-but-got-some-errors/ Share on other sites More sharing options...
hennzo Posted December 8, 2010 Share Posted December 8, 2010 Hi, I haven't taken a lot of time to go through your code, but first change this: $answer = $_POST['submit']; by: $answer = $_POST['answer']; Quote Link to comment https://forums.phpfreaks.com/topic/220968-building-a-quiz-script-but-got-some-errors/#findComment-1144249 Share on other sites More sharing options...
sasa Posted December 8, 2010 Share Posted December 8, 2010 you newer open form tag (but you close it) values of $_POST['answer'] is 'Yes' or 'No' NOT 1 or 2 Quote Link to comment https://forums.phpfreaks.com/topic/220968-building-a-quiz-script-but-got-some-errors/#findComment-1144288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.