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 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Questions on PHP page for test/quiz I am building

    Ok I have the main sign in page able to login and mysql query the user table from my php code with no errors.

    But, for using the test.php page, how would I go about continuing to pull my variable $username onto that page so that it's pulling from the db that user account? Also, when submitting I want the db to have an insert statement that inputs into that user table the score/grade and also email the person in charge of this test/quiz.

    I am still a noob at this fyi.

    my test.php page
    PHP Code:
    <?php

    session_name
    ("Test");
    session_start();
    require_once(
    'questionsandanswers.php');
    require_once(
    'functions.php');
    if (!isset(
    $_POST['submitter'])) {

        if(isset(
    $_POST['register'])) {
            
    $myusername trim(strip_tags(stripslashes($_POST['myusername'])));
            
    $file "leaders.xml";
             
    $xml simplexml_load_file($file);
            foreach(
    $xml->user as $user) {
                if (
    $user->name == $myusername) {
                 
    $_SESSION['error'] = 'That name is already registered, please choose another.';
                 
    header('Location: index.php');
                 exit();
                 }
             }
        
            
    $_SESSION['user'] = $myusername;
            
    $_SESSION['score'] = 0;
            
    $_SESSION['correct'] = array(); 
            
    $_SESSION['wrong'] = array();
            
    $_SESSION['finished'] = 'no';
            if (isset(
    $_SESSION['error']))
            unset(
    $_SESSION['error']); 
            
    $num 0;
        } else {
            
    $random rand(1,1000);
            
    $_SESSION['user'] = 'Anon'$random;
            
    $_SESSION['score'] = 0;
            
    $_SESSION['correct'] = array(); 
            
    $_SESSION['wrong'] = array(); 
            
    $_SESSION['finished'] = 'no';
            
    $num 0;
        }
    } else {
        
    $num = (int) $_POST['num'];
        
    $postedanswers str_replace("_"," ",$_POST['answers']);
        if (
    $postedanswers == $answers[$num]['0']) {
            
    $_SESSION['score']++;
            
    $_SESSION['correct'][] = $postedanswers
        } else {
            
    $_SESSION['wrong'][] = $postedanswers;
        } 
        if (
    $num count($questions)-1) {
            
    $num++;
        } else {
            
    $last true;
            
    $_SESSION['finished'] = 'yes';
        }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <title>IT Security Awareness Training Test</title>
    <?php 
    if (!isset($last)) {
        echo 
    "<script type=\"text/javascript\" src=\"form.js\"></script>";
    }
    ?>
    </head>
    <body>
    <div id="wrapper">
    <div id="intro">
    <?php if(isset($_SESSION['user'])) echo "<h4>Current tester: {$_SESSION['user']}</h4>"?>
    </div><!--intro-->
    <div id="quiz">
    <?php 
    if (!isset($last)) {  ?>
    <h2><?php echo $num+1?>:</h2>
    <p><strong><?php echo $questions[$num]; ?></strong></p>
    <form id="questionBox" method="post" action="test.php">
    <ul>
    <?php 
    $pattern 
    ' ';
    $replace '_';
    shuffle_assoc($answers[$num]);
    foreach (
    $answers[$num] as $answer) {
        
    $answer2 str_replace($pattern,$replace,$answer);
        echo 
    "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n";
        echo 
    "<label for=\"$answer2\">$answer</label></li>\n";
    }
    ?>
    </ul>
    <p><input type="hidden" name="num" value="<?php echo $num?>" />
    <input type="hidden" name="submitter" value="TRUE" />
    <input type="submit" id="submit" name="submit" value="Submit Answer" /></p>
    </form>
    <?php } else { 
    $file "leaders.xml";
     
    $xml simplexml_load_file($file);
     
    $user $xml->addChild('user');
     
    $uname $user->addChild('name',$_SESSION['user']);
     
    $uscore $user->addChild('score',$_SESSION['score']);
     
    $xml->asXML("leaders.xml");
     
    echo 
    "<h2 id=\"score\">{$_SESSION['user']}, your final score is:</h2>\n
     <h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>"
    ;
     if(
    $_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
     if((
    $_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
     if((
    $_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
     if(
    $_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";
     echo 
    "<p id=\"compare\"><a href=\"results.php\">See how you compare! <img src=\"images/arrow.png\" /></a></p>";
    }
    ?>
    </div><!--quiz-->
    </div><!--wrapper-->
    </body>
    </html>
    Last edited by astralart; 07-01-2009 at 08:42 PM.


 

Posting Permissions

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