Jump to content

[SOLVED] My script is useless unless you're a fortune teller or time lord...


eugeniu

Recommended Posts

I recently made a script that shows a picture selected from a text database, and has the person input what it says. On the next page it should say "Correct!" or "Incorrect..." and a comment on the specific image based on the previous image.

 

This is what I have right now:

<?php

//Set the database filename
$db = "words.txt";

//Read the file and store the lines in an array
$lines = file($db);
//Count the lines in the array
$lc = count($lines) - 1;
//echo $lc;
//Select a random line
$rl = rand(0, $lc);
//echo "\n" . $rl;

//Separate the items on the chosen line
$sep = explode(":", $lines[$rl]);

$trans = strtolower($sep[0]); //English pronunciation (lower case)
//echo "\n" . $trans;
$desc = $sep[1]; //Description of word
//echo "\n" . $desc;

include ("header.php");
include ("header2.php");

if (isset($_POST['submit'])) {
$answer = strtolower($_POST['answer']);
if ($answer == $trans) {
	echo "<br><b><font color=\"#28ab00\" size=\"1\" face=\"Verdana\">Correct! </b><font color=\"000000\">";
	echo $desc;
	echo "<br></font>";
} else {
	echo "<br><b><font color=\"#c00000\" size=\"1\" face=\"Verdana\">Incorrect... </b><font color=\"000000\">";
	echo $desc;
	echo "<br></font>";
}
}

echo "<img src=\"words/" . $trans . ".png\"><br>
<form id=\"quiz\" action=\"words.php\" method=\"post\">
<input type=\"text\" name=\"answer\" size=\"8\" maxlength=\"30\">
<input type=\"submit\" name=\"submit\" value=\">>\">
</form><br><a href=\"http://kanaquest.com/main.php\">Home</a> | <a href=\"instructions.php\">Instructions</a>";

include ("footer2.php");
include ("footer.php");
?>

 

But sadly, the script determines if you are correct based on the next image. So if the first page has picture "A" and you enter "A", but on the next page, the picture is picture "B" then the script compares answer "A" with picture "B". Which is useless.

 

How can I rewrite this so it compares the given answer with the right image?

save the image info in a session as soon as it's selected. retrieve the session value when you do the comparison.

 

OR

 

place the image info in a hidden field of your form and retrieve the passed info on the next page.

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.