Jump to content

PHP reloading when clicking back button


Go to solution Solved by requinix,

Recommended Posts

So, I just don't know where to post this question. I've written a quiz program where 50 English vocab words are pulled from a dB and displayed on one page with the order scrambled. My students then type the Spanish translation for each. When they submit the form the next page grades their work. They have to get a 90 or better for the grade to be automatically recorded, so I want them to click the back button and go back and make corrections. The problem is when they use a Chrome browser and click the back button the program re-sorts the order of questions. It leaves the order of their answers the same. So what they answered for number 1 is still in the field for number 1, but the questions (the English vocab word) is different. Chrome does this consistently so I thought this was a Chrome issue only. Then one of my students said the same thing happened to her when using Safari.

So here is my question, how can I stop Chrome (or any other browser) from re-running my server-side PHP scripts (re-sorting the vocab array) when clicking the back button?

 

Here is the code for students taking the quiz:

<form method="post" action="portal.php?load=vquiz_grade" style="margin-left: 50px; line-height: 30px;">
<table>
    <?
    $count = 1;
    $query = "SELECT * FROM inno_memorize WHERE uname = 'admiralq' AND subject = '$subject' AND title = '$title' ORDER BY subject, title";
    $result= mysql_query($query) or die("Could not perform query: ".mysql_error());
    while ($row = mysql_fetch_array($result)){
        $ans[$count]['def'] = $row['definition'];
        $ans[$count]['word'] = $row['vocab_word'];
        $count++;
    }

    $max = $count-1;
    shuffle($ans);
    
    $count = 0;
    while ($count < $max){
    
        // Search for () and eliminate along with contents
        $loc1 = strpos($ans[$count]['word'], "(");
        $loc2 = strpos($ans[$count]['word'], ")");
        
        
        if ($loc1 != "" AND $loc2 != ""){
                
            // string length
            $length = strlen($ans[$count]['word']);
            // from loc2 to end
            $lcount = $length - $loc2;
            
            // Dump location points and all text inbetween
            $ans[$count]['word'] = substr($ans[$count]['word'], 0, ($loc1-1)).substr($ans[$count]['word'], ($loc2+1));
                        
        }
        print "<tr><td>".($count+1).". <textarea id=\"kelp".$count."\" cols=25 rows=1 name=\"answer".$count."\" style=\"resize: none;\" autocomplete=\"off\"></textarea>&nbsp;</td>";
        
        print "<td>".$ans[$count]['def']."</td>";
        
        print "<td><input type=\"hidden\" name=\"correct".$count."\" value=\"".$ans[$count]['word']."\"></td>";

        ?>
        <td>
            <span style="margin: 0px 0px 20px 50px;">
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&aacute;');">&aacute;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&eacute;');">&eacute;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&iacute;');">&iacute;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&oacute;');">&oacute;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&uacute;');">&uacute;</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&ntilde;');">&ntilde;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&iquest;');">&iquest;</button>
            <button type="button" onclick="insertAtCursor('kelp<?print $count?>', '&iexcl;');">&iexcl;</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </span>
        </td>
        </tr>
        <?

        //print "<br>";
        $count++;
    }


    ?>
</table>

    <input type="hidden" name="max" value="<?print $max?>">
    <input type="hidden" name="title" value="<?print $title?>">
    <input type="hidden" name="subject" value="<?print $subject?>">
    <input type="hidden" name="vlist" value="<?print $vlist?>">
    <input type="hidden" name="lname" value="<?print $secure_lname?>">
    <input type="hidden" name="fname" value="<?print $secure_fname?>">
    <input type="hidden" name="secure_uname" value="<?print $secure_uname?>">
    <input type="hidden" name="key_code" value="<?print $key_code?>">
    <input type="hidden" name="vocab_quiz" value="1">
    <input type="hidden" name="id" value="<?print $id?>">
    <input type="submit">    
</form>
 

And here is the code showing them what they missed.

<?
$count = 0;
$wcount = 0;
$rcount = 0;

//    Total grade
while ($count < $max){
    if (trim(strtolower(${'answer'.$count})) == trim(strtolower(${'correct'.$count}))){$rcount++;}
    else {$wcount++;}
    $count++;
}

print "You missed ".$wcount." words.<br>";
$grade = $rcount / ($rcount+$wcount)*100;
print "Your grade: ".round($grade)."<br>";


$count = 0;
$wrong = 0;

print "<table>";
print "<tr><td height=\"10\"></td></tr>";
while ($count < $max){
    
    $tcolor = "0a0a0a";

    // Grade response and set color
    if (trim(strtolower(${'answer'.$count})) == trim(strtolower(${'correct'.$count}))){$tcolor = "ff7777";}
    else {$tcolor = "000000"; $wrong = 1;}

    if ($wrong == 1){
        print "<tr>";
        print "<td>".($count+1).".&nbsp;</td>";
        print "<td><span style=\"color: #".$tcolor.";\">".${'answer'.$count}."</span></td>";
        print "<td><span style=\"color: #ff5555;\"> - ".${'correct'.$count}."</span></td>";
        print "</tr>";
        $wrong = 0;
    }
    
    $count++;
    
}
print "</table>";

if ($grade >= 90){

    $query = "SELECT * FROM inno_vocab_assignments WHERE key_code = '$key_code' AND uname = '$secure_uname' AND teacher = '$teacher'";
    $result= mysql_query($query) or die("Could not perform query: ".mysql_error());
    if (mysql_num_rows($result) == 0){

        //    Prevent student from changing vocab list so as to do an easier list
        $query = "SELECT * FROM inno_grading_period WHERE uname = '{$_SESSION['student']['teacher']}' AND voc_title = '$title' AND subject = '$subject'";
        $result= mysql_query($query) or die("Could not perform query: ".mysql_error());
        if (mysql_num_rows($result) > 0){

            //    Insert grade
            $query = "INSERT INTO inno_vocab_assignments (uname, course_title, list_title, type, stamp, grade, key_code, course_id, teacher, lname, fname) VALUES ('$secure_uname', '$subject', '$title', 'typing', '".date("U")."', '$grade', '$key_code', '$course_id', '$teacher', '$lname', '$fname')";
            $result= mysql_query($query) or die("Could not perform query: ".mysql_error());
        
            print "<br><b>RESULT: Congratulations! Your quiz was recorded.</b><br><br><br><br>";
        }
        else{
            print "<span style=\"color: #ff5555; font-weight: bold;\">This vocab list was not assigned! The teacher has been notified of this attempt!<br>The assignment has NOT been recorded!</span>";
        }

    }
    else {print "<br><b>THIS HAS ALREADY BEEN RECORDED!<br>You cannot submit the same assignment more than once.</b>";}
    
}
else{
    
    print "<br><b>RESULT: Your score was below a 90 and so was not recorded. Click back to make corrections.</b><br><br><br><br><br>";
    
}


?>
 

 

Thank you!

 

 

Link to post
Share on other sites
  • Solution

Given that browsers tend to decide for themselves whether to use a cached version of a page or not (though you can influence that), the most reliable method would be to do two things:

1. Randomize the order of questions in a reproduceable way. shuffle() is always random and you can't control it, but if you moved the randomization into your SELECT query (which is possible) then you could get the same "random" ordering every time. You would then need some identifier in the URL that you can use or turn into a number suitable for this purpose. Exactly how depends.
2. Instead of telling people to go back, give them a link to click to "return" to the questions. This is a great idea because not only do you eliminate the Back button problem, you can give people a link that shows what their answers were and which ones were correct or incorrect. (If you want to.)

Link to post
Share on other sites

To me that would be more ideal for a client side web app (like JavaScript) than a server-side app (PHP). They wouldn't have to use the back button at all and you could even use JavaScript, Ajax and PHP if you want to use a MySQL table.

To give you an idea here is an example of a trivia game that I wrote for my website - https://www.miniaturephotographer.com/game.php

I'm still developing it, but it works pretty darn good and you can do all sorts of things to it.

 

 

Edited by Strider64
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.