Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: PHP Quiz Pages

Reply  
Thread Tools
Mogul126's Avatar
Junior Member with 5 posts.
 
Join Date: Mar 2010
Experience: Advanced
12-Mar-2010, 10:28 AM #1
Solved: PHP Quiz Pages
Hi everyone, I'm the webmaster for my university's library. For some time now, they've had me working on updating an old "Information Literacy Tutorial" that was originally a University of Texas project, back in the early 00s. Anyways, we were just about ready to launch it, when the geniuses in our IT department decided to do something with the PHP on the server that our page was on. Long story short, it broke everything.

I've been through most of the other pages and managed to fix them all, but there is one block of pages that I cannot get to work, those are the pages for the quizzes at the end of each section. I'm at a loss for why these have suddenly stopped working, I'm not really that experienced with PHP. Can anyone spot the problem(s) in these?

First question in one of the quizzes:
Code:
<?php

require_once "../../include/functions.php";
$question = 1;

//Check that question hasn't been answered
$msg = chk_question($next_question);
if ($msg != "")
    $next_question = 1;
 
//Check that at least one response has been entered
if (count($q1) < 2 && $next_question == 2){

    $err_msg = "Please select two choices";
    $next_question = 1;
    
}

if (count($q1) > 2 && $next_question == 2){

    $err_msg = "Please select only two choices";
    $next_question = 1;
    
}
//Print and record question results
if ($next_question == 2){

    $total = count($q1);
    
    for($i = 0; $i < $total; $i++){
    
        $response .= "q1:".$q1[$i];
        
        if ($i < ($total - 1))
            $response .= "&";
            
    }
    
    $msg .= "<h4>Answers to Question One:</h4>";
    
    if ($q1[0] == "journal" && $q1[1] == "index"){
    
        $msg .= "<b>You are correct.</b>
        Periodical indexes 
        help you determine which journals have scholarly articles on 
        your topic.<br>";
        $correct = 2;
        
    }else if ($q1[0] == "journal" || $q1[1] == "journal"){
    
        $msg .= "<b>Well, you are half right.</b>
        Journals contain scholarly articles. The other correct answer is
        the source that indexes those journal articles.<br>";
        $correct = 1;
        
    }else if ($q1[0] == "index" || $q1[1] == "index"){
    
        $msg .= "<b>Well, you are half right.</b>
        Periodical indexes do help you locate scholarly articles. But
        magazines, newspapers and email are not the best sources for
        finding scholarly materials.<br>";
        $correct = 1;
    
    }else{
    
        $msg .= "<b>Hmmmm.</b>
        Neither of these answers are correct. Perhaps the phrase of 
        \"scholarly information\" was confusing. An example of scholarly 
        information is a journal article written by a person who has done 
        research in their area of expertise. Often these articles are 
        indexed by periodical indexes so you can easily find them.<br>";
        $correct = 0;
        
    }
    
    $response = $_COOKIE["response"];
    $response .= "+q1:".$q1[0]."&q1:".$q1[1];
    setcookie ("response", $response);
    setcookie ("next", "2");
    setcookie ("ans[0]", $correct);
    $msg .= "<p><div align='center'><a href='quiz.php'>Go on to question 2</a></div></p>";
    
}

require_once "../../include/quiz_header.html";

?>
 
<h2>Question One:</h2>

<h3 align="CENTER">
Imagine you have an assignment to write a paper based on scholarly<br>
information. Which would be the most appropriate sources to use? 
(Choose two.)</h3>

<form method="POST" action="q1.php">

<div align="CENTER">

<table border="0" width="60%" cellspacing="2" cellpadding="2">
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "journal"):
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"journal\" CHECKED>";
    else: 
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"journal\">";
    endif;
    ?>
    A. Journal
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "Magazine" || $q1[1] == "Magazine" ):
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\" CHECKED>";
    else: 
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\">";
    endif;
    ?>
    B. Magazine
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "Newspaper" || $q1[1] == "Newspaper" ):
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\" CHECKED>";
    else: 
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\">";
    endif;
    ?>
    C. Newspaper
  </tr>
  <tr> 
    <td> 
    <?php
    if ($q1[0] == "index" || $q1[1] == "index" ):
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"index\" CHECKED>";
    else: 
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"index\">";
    endif;
    ?>
    D. Periodical index
  </tr>
  <tr>
    <td>
    <?php
    if ($q1[1] == "email" ):
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"email\" CHECKED>";
    else: 
        print "<input type=\"checkbox\" name=\"q1[]\" value=\"email\">";
    endif;
    ?>
          E. Email 
      </tr>
  </table>
<p>
<input type="hidden" name ="next_question" value="2">
<?php 
    if ($msg == ""){ 
        //is msg != "" then the form has been submitted
        print "<input type=\"submit\" value=\"submit\">";
    } 
?>
</p>
</div>
</form>
<?php
//Print error message or results 
if ($err_msg != ""){
    print "
    <script language=\"JavaScript\">
    <!--
    onLoad=window.alert(\"$err_msg\")
    -->
    </script>";
}
print "$msg";
require_once "../../include/quiz_footer.html";
?>
The included "Functions" file:
Code:
<?php
function chk_question( $question ){

    $msg = "";
    $next_q = $_COOKIE["next"];
    
    if( $next_q == "" )
        $next_q = 1;   
        
    if( ( $next_q  >= $question ) && $question != "" ){
    
        $msg .= "<div class='redText' align=\"CENTER\">You have already answered this question.<br>
        Please move on to <a href='quiz.html'>question $next_q</a></div>";
        
    }
    
    return $msg;
    
}
  
function percent( $num, $denom ){

    if( $denom == 0 ) /* undefined */
        return 0;
    else
        return round ( $num / $denom * 100 )."%";
        
}

function colorPercent( $num, $denom ){

    $percent = percent( $num, $denom );
    
    if( $percent > 75 )
        $msg = "<span class='greenText'>";
    else if ( $percent > 50 )
        $msg = "<span class=\"orangeText\">";
    else
        $msg = "<span class=\"redText\">";
    $msg .= $percent."</span>";
    
    return $msg;
    
}

function formatQuiz( $quiz_ary, $correct_ary ){
    
    $total_questions = 0;
    $total_correct = 0;
    $msg = "
    <table width=60% cols=4 cellpadding=0 cellspacing=1 border=0>
    <tr><th align='left'>Question</td><th align='center'># Correct</th>
    <th align='right'># Incorrect</th><th align='right'>Percent</th></tr>
    <tr><td colspan=4><hr noshade></td></tr>";
    
    for ( $x = 0; $x < count( $quiz_ary ); $x++ ){
    
        $msg .= "<tr><td align='left'>".( $x + 1 )."</td><td align='center'>".$quiz_ary[$x]."</td>
        <td align='right'>".( $correct_ary[$x] - $quiz_ary[$x] )."</td>
        <td align='right'>".colorPercent( $quiz_ary[$x], $correct_ary[$x] )."</td></tr>\n";
        
        $total_correct += $quiz_ary[$x];
        $total_incorrect += $correct_ary[$x] - $quiz_ary[$x];
        $total += $correct_ary[$x];
        
    }
    
    $msg .= "<tr><td colspan=4><hr noshade></td></tr>
    <tr><th align='left'>Total</th><th align='center'>$total_correct</th><th align='right'>$total_incorrect</th>
    <th align='right'>".colorPercent( $total_correct, $total )."</th></tr></table>";  
    
    return $msg;
    
}

function getUserName ( $reg_id ){

    /* Print user name on report */
    if ( $reg_id == "" ):
        $cn = "Guest";
    else:
        /* mySql Operations */
        require "../../include/db_info.php";           //Database Vars
        $link = mysql_connect( $host, $uname, $pass )     //Connect to the Database
            or die ( "Could not connect to database" );
        mysql_select_db( $db )  
            or die ( "Could not select database" );
        $sql =  "SELECT first_name, last_name
                 FROM person
                 WHERE person_id = $reg_id";
                 $result = mysql_query( $sql );
                 $row = mysql_fetch_row( $result );
                 $cn = $row[0]." ".$row[1];
    endif;
    
    return $cn;
    
}
?>
Let me know if you need any more information than these files provide. The PHP version on the server is 5.1.6. Thanks!
ehymel's Avatar
Senior Member with 660 posts.
 
Join Date: Aug 2007
Location: Texas
12-Mar-2010, 01:06 PM #2
It would be very helpful to know what error message is being generated. In fact, this will likely tell you exactly where the problem lies. The error messages are probably in the web server log files.
Mogul126's Avatar
Junior Member with 5 posts.
 
Join Date: Mar 2010
Experience: Advanced
24-Mar-2010, 04:22 PM #3
Sorry for the thread resurrection, I was on spring break and there's no way I was doing any work on this thorn in my side while I was trying to relax.

There's a problem with your suggestion, ehymel, that being that I don't have access to any error logs, nor does it display any sort of error; it just reloads the page with no change at all. The access problem is due to how this particular server is set up. The server is apparently on a virtual machine (if my professors are to be believed) that is running as a Polaris/AFS server. The only means that I have of accessing the server is through SSH; I only have access to my personal student directory, and the directory that the PHP, HTML, CSS, etc. files that are part of the website are stored. Is there any other way that I could find the error, some way of putting debugging statements into the code or something?
ehymel's Avatar
Senior Member with 660 posts.
 
Join Date: Aug 2007
Location: Texas
25-Mar-2010, 01:29 AM #4
Try adding this to your .htaccess file:

PHP Code:
php_value display_errors 1 
This should only be used during development for debugging, but it's very useful!
Mogul126's Avatar
Junior Member with 5 posts.
 
Join Date: Mar 2010
Experience: Advanced
27-Mar-2010, 01:50 PM #5
I did as you suggested, and the page that was giving me trouble spit out a bunch of undefined variable notices. I added blocks at the beginning of the page such as this:
Code:
if( !isset( $variable ) ){
    $variable = 0;
}
This managed to get rid of all of the error notifications, but the page still just reloads itself when the "Submit" button is clicked, seemingly without doing anything.
ehymel's Avatar
Senior Member with 660 posts.
 
Join Date: Aug 2007
Location: Texas
28-Mar-2010, 06:51 PM #6
I'm not really sure what you mean by "not working"... that could mean not displaying at all, or the code is not processing input correctly, or etc...

After studying your code, I see that the there is a big problem, but maybe you haven't posted the full code for us to see. The problem is that you never define the variable q1. You also don't define the variable next_question. From your form code, it looks like they should be defined from there. Something like this:

PHP Code:
$q1 $_POST["q1"];
$next_question $_POST["next_question"]; 
To avoid the error notices you saw about undefined variables, I use something like this:

PHP Code:
$q1 = (isset($_POST["q1"])) ? $_POST["q1"] : "";
$next_question = (isset($_POST["next_question"])) ? $_POST["next_question"] : "1"
This is a more compact form of the IF... THEN method you use to define variables.

If this does not fix your problem, please post the full code of q1.php as well as the formatting include files that start and end the page.
__________________
E.
Mogul126's Avatar
Junior Member with 5 posts.
 
Join Date: Mar 2010
Experience: Advanced
31-Mar-2010, 05:36 PM #7
Defining the variables in either way did nothing to solve the problem. Here's the code for the entire q1.php file, and the header/footer files.

q1.php
PHP Code:
<?php
require_once "../../include/functions.php";

$question 1;

if( !isset( 
$next_question ) ){
    
$next_question 0;
}

if( !isset( 
$q1 ) ){
    
$q1[0] = "";
    
$q1[1] = "";
}    
    
//Check that question hasn't been answered
$msg chk_question($next_question);
if (
$msg != "") {
 
$next_question 1;
}
//Check that at least one response has been entered
if (count($q1) < && $next_question == 2){
    
$err_msg "Please select two choices";
    
$next_question 1;
}
if (
count($q1) > && $next_question == 2){
    
$err_msg "Please select only two choices";
    
$next_question 1;
}
//Print and record question results
if ($next_question == 2){
    
$total count($q1);
    for(
$i 0$i $total$i++){
        
$response .= "q1:".$q1[$i];
        if (
$i < ($total 1))
            
$response .= "&";
    }
    
$msg .= "<h4>Answers to Question One:</h4>";
    if (
$q1[0] == "journal" && $q1[1] == "index") {
        
$msg .= "<b>You are correct.</b>
        Periodical indexes 
        help you determine which journals have scholarly articles on 
        your topic.<br>"
;
        
$correct 2;
    } else if (
$q1[0] == "journal" || $q1[1] == "journal") {
        
$msg .= "<b>Well, you are half right.</b>
        Journals contain scholarly articles. The other correct answer is
        the source that indexes those journal articles.<br>"
;
        
$correct 1;
    } else if (
$q1[0] == "index" || $q1[1] == "index") {
        
$msg .= "<b>Well, you are half right.</b>
        Periodical indexes do help you locate scholarly articles. But
        magazines, newspapers and email are not the best sources for
        finding scholarly materials.<br>"
;
        
$correct 1;
    } else {
        
$msg .= "<b>Hmmmm.</b>
        Neither of these answers are correct. Perhaps the phrase of 
        \"scholarly information\" was confusing. An example of scholarly 
        information is a journal article written by a person who has done 
        research in their area of expertise. Often these articles are 
        indexed by periodical indexes so you can easily find them.<br>"
;
        
$correct 0;
    }
    
$response $_COOKIE["response"];
    
$response .= "+q1:".$q1[0]."&q1:".$q1[1];
    
setcookie ("response"$response);
    
setcookie ("next""2");
    
setcookie ("ans[0]"$correct);
    
$msg .= "<p><div align='center'><a href='quiz.php'>Go on to question 2</a></div></p>";
}
require_once 
"../../include/quiz_header.php";
?>
 
<h2>Question One:</h2>
<h3 align="CENTER">
Imagine you have an assignment to write a paper based on scholarly<br>
information. Which would be the most appropriate sources to use? 
(Choose two.)</h3>
<form method="POST" action="q1.php">
<div align="CENTER">
<table border="0" width="60%" cellspacing="2" cellpadding="2">
  <tr> 
    <td> 
    <?php
    
if ($q1[0] == "journal"):
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"journal\" CHECKED>";
    else: 
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"journal\">";
    endif;
    
?>
    A. Journal
  </tr>
  <tr> 
    <td> 
    <?php
    
if ($q1[0] == "Magazine" || $q1[1] == "Magazine" ):
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\" CHECKED>";
    else: 
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"Magazine\">";
    endif;
    
?>
    B. Magazine
  </tr>
  <tr> 
    <td> 
    <?php
    
if ($q1[0] == "Newspaper" || $q1[1] == "Newspaper" ):
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\" CHECKED>";
    else: 
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"Newspaper\">";
    endif;
    
?>
    C. Newspaper
  </tr>
  <tr> 
    <td> 
    <?php
    
if ($q1[0] == "index" || $q1[1] == "index" ):
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"index\" CHECKED>";
    else: 
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"index\">";
    endif;
    
?>
    D. Periodical index
  </tr>
  <tr>
    <td>
    <?php
    
if ($q1[1] == "email" ):
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"email\" CHECKED>";
    else: 
        print 
"<input type=\"checkbox\" name=\"q1[]\" value=\"email\">";
    endif;
    
?>
          E. Email 
      </tr>
  </table>
<p>
<input type="hidden" name ="next_question" value="2">
<?php 
    
if ($msg == ""){ 
        
//is msg != "" then the form has been submitted
        
print "<input type=\"submit\" value=\"submit\">";
    } 
?>
</p>
</div>
</form>
<?php
//Print error message or results 
if ($err_msg != ""){
    print 
"
    <script language=\"JavaScript\">
    <!--
    onLoad=window.alert(\"$err_msg\")
    -->
    </script>"
;
}
print 
"$msg";
require_once 
"../../include/quiz_footer.php";
?>
quiz_header.php
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<title>TILT Quiz - Question <?php print $question ?></title>
<link REL=stylesheet HREF="tiltstyle.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFCEB">
quiz_footer.php
PHP Code:
</body>
</
html
ehymel's Avatar
Senior Member with 660 posts.
 
Join Date: Aug 2007
Location: Texas
01-Apr-2010, 12:05 AM #8
I'm still not sure what you mean by "not working". All works perfectly for me if I cut and paste your code, *except* within the first few lines where grab the q1 and next_question variables from $_POST, as here:

PHP Code:
<?php

require_once "../../include/functions.php";

$q1 = (isset($_POST["q1"])) ? $_POST["q1"] : '';
$next_question = (isset($_POST["next_question"])) ? $_POST["next_question"] : '';

$question 1;

if( !isset( 
$next_question ) ){
    
$next_question 0;
}

if( !isset( 
$q1 ) ){
    
$q1[0] = "";
    
$q1[1] = "";
}

...

Last edited by ehymel; 01-Apr-2010 at 12:06 AM.. Reason: fix include path to match original post
Eriksrocks's Avatar
Computer Specs
Distinguished Member with 2,198 posts.
 
Join Date: Aug 2005
Location: Minnesota
Experience: Advanced
02-Apr-2010, 02:09 AM #9
Check the include path.
avisitor's Avatar
Computer Specs
Senior Member with 1,712 posts.
 
Join Date: Jul 2008
Location: Chicago, IL
Experience: Advanced
02-Apr-2010, 10:04 AM #10
My best guess is that you got upgraded from PHP <= 4.2.0 so what got changed is register globals.

Humor me and try adding the following line to your .htaccess file:

Code:
php_flag register_globals on 


If that fixes the problem, remove the line and then at the beginning of your php file, you need to grab all the problem variables, a la
Code:
if(!isset($var)) 
{
$var = $_REQUEST["var"]
}
__________________
Austin

Please refresh, I edit my posts often.

Last edited by avisitor; 02-Apr-2010 at 08:54 PM..
Mogul126's Avatar
Junior Member with 5 posts.
 
Join Date: Mar 2010
Experience: Advanced
02-Apr-2010, 05:17 PM #11
Adding that flag to .htaccess fixed all of the issues. So all I need to do is add the "if(!isset($var))" lines to the top of each problematic PHP file?
avisitor's Avatar
Computer Specs
Senior Member with 1,712 posts.
 
Join Date: Jul 2008
Location: Chicago, IL
Experience: Advanced
02-Apr-2010, 08:53 PM #12
Ok, now that we know what the problem is, it's pretty easy to fix. You have two options:

1) Leave register globals on. You need to look into the security implications of this.
2) Write code that doesn't use register globals.

I'd recommend the second. To go about this, you need to add code like for every variable that you expect to be passed in via a GET string or from another page via POST.

For example, for foo:

Code:
$foo = $_REQUEST["foo"];
Specifically, in your code, I see next_question, so that would be

Code:
$next_question = $_REQUEST["next_question"];
And so on.
__________________
Austin

Please refresh, I edit my posts often.
Grob needs help's Avatar
Computer Specs
Member with 280 posts.
 
Join Date: Mar 2009
Location: marldon
Experience: Advanced
04-Apr-2010, 03:08 PM #13
well if you notice some of the if statements do not open with a { i would go through and check that all the if statements have a { at the start and a } at the end, as this would have worked in older versions of php but the newer versions do not except it at all.
ehymel's Avatar
Senior Member with 660 posts.
 
Join Date: Aug 2007
Location: Texas
05-Apr-2010, 11:21 PM #14
Quote:
Originally Posted by Grob needs help View Post
well if you notice some of the if statements do not open with a { i would go through and check that all the if statements have a { at the start and a } at the end, as this would have worked in older versions of php but the newer versions do not except it at all.
I'm not sure what you're referring to here. The if...then statements are all written in correct syntax. You only need the { } brackets around statements that cover multiple lines. They are otherwise unnecessary. See the first example on the php.net website (http://us2.php.net/manual/en/control-structures.if.php) describing the if statement for reference.
__________________
E.
avisitor's Avatar
Computer Specs
Senior Member with 1,712 posts.
 
Join Date: Jul 2008
Location: Chicago, IL
Experience: Advanced
06-Apr-2010, 11:45 AM #15
Yes, once again, all the if statements are correct.

The only problems with the code is the assumption on register globals. It's easily fixed as in my earlier post. Or leave register globals on.
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:00 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.