Click to See Complete Forum and Search --> : Verify A Text Feild
The Little Guy
10-09-2005, 08:53 PM
I have a little game that I am in the process of making, and I want PHP to read what a user put into a text feild, and If the answer is wrong, It will tell them so, and If it is right, they get to move on to the next clue.
The Little Guy
10-10-2005, 03:29 PM
anyone have an idea?
Maybe I can explain a little more. First off here is a link to what I am doing: http://ryanad.coolinc.info/hunter/
Now that you can see it, here is what will happen. you read the clue, figure it out, put your answer into the textfeild that I have provided, press the "Check" button or press enter. If you have ented in the correct answer, you will be taken to the next clue. If you entered in the In-Correct answer, it would print onto the screen/page in red "Sorry Try Again".
Does this help a little more? I don't want to use javascript for this, because people can see the name of the next page if they look at the javascript.
Hi. I was about to post a very similar query to yours, so if I may I'll tag along with you. Mine also is a 'hunt', so to speak, but unlike yours it requires the visitor to literally find an image on the website and then answer a question regarding it.
What I am trying to create is a simple single field text box in which the visitor will type his answer. If it is correct, the Submit will send him to the winning page (so to speak), but if the visitor types anything BUT the correct answer, he will be sent into page n.2, try-again type of page.
Javascript is out of the question, the relative urls and the correct answer itself would show, so Php must be the way.
Pestering people left right and center, I've come up with this:
$ans = $_POST["txtAnswer"]; // this is data from the previous page
if( $ans == "A" ) // the correct answer
{
header("Location: <http://www.mysite.com/correctanswer.php>");
}
else {
header("Location: <http://www.mysite.com/wronganswer.php>");
}
which would seem to function. My problem is:
a) I can't even recognize if my server enables php.
b) would the above (or similar) php code be nested among the original page's html?
c) please be patient with a true newbie to php - and all enlightments are extremely appreciated!
The Little Guy, maybe the above code can somewhat help you a bit - I really do not know.
Thanks to anyone who will spend a few minutes to knock some knowledge into my head...!
purefan
10-10-2005, 06:16 PM
Greetings.
well knowing if your server has php enabled is quite easy. just do a simple .php upload it and open it in your browser.
the simplest could be:
<?php
echo "<html><body>php enabled</html></body>";
?>
and if you can see the "php enabled" text ONLY [without the php code] then you're set to go.
Now, DC1 are you using frames on your site??
No, no frames.
I am now loading up the above code to check php enabling - thanks for walking me through this - I'll post the result here within minutes.
Oh great. Server not enabled. No wonder all my tries were total flops... beside ignorance, of course.
I loaded your code in a folder, went straight to it and nicht -Source shows your code, page completely white.
Now how in the world am I going to manage that simple field form without letting code show in source? Would Link rel-ling help me out?
Sigh.
NogDog
10-10-2005, 06:55 PM
One way to do it: all in one file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Quiz</title>
<style type="text/css">
fieldset {
width: 20em;
margin: 0 auto;
}
#result {
margin: 0.5em;
padding: 0.5em;
border: solid 1px black;
color: #cc3333;
text-align: center;
}
</style>
</head>
<body>
<?php
# quiz data:
$data = array(
1 => array(
'question' => 'What is your favorite color?',
'answer' => 'Blue'),
2 => array(
'question' => 'What is your quest?',
'answer' => 'Holy Grail'),
3 => array(
'question' => 'What is the average unladen airspeed of a swallow?',
'answer' => 'African or European?'));
# set curret question number:
$nbr = (isset($_GET['nbr']) and array_key_exists( $_GET['nbr'], $data)) ?
$_GET['nbr'] : 1;
# display the form:
echo <<<EOD
<form action='{$_SERVER['PHP_SELF']}?nbr=$nbr' method='post'>
<fieldset>
<legend>Question #$nbr</legend>
<p>{$data[$nbr]['question']}</p>
<p style='text-align: center;'><input type='text' name='answer'></p>
<p style='text-align: center;'><input type='submit' value='Submit'></p>
EOD;
# if answer was given, show result:
echo "<p id='result'>";
if(isset($_POST['answer']))
{
$result=(strtolower($_POST['answer']) == strtolower($data[$nbr]['answer'])) ?
"Correct!" : "Sorry, wrong answer.";
echo "$result";
}
echo " </p>\n";
# link to next question (if there is one):
echo "<p style='text-align: center;'>\n";
if(array_key_exists($nbr - 1, $data))
{
echo "<a href='{$_SERVER['PHP_SELF']}?nbr=".($nbr - 1)."'>Previous</a> ";
}
if(array_key_exists($nbr + 1, $data))
{
echo " <a href='{$_SERVER['PHP_SELF']}?nbr=".($nbr + 1)."'>Next</a>";
}
echo <<<EOD
</p>
</fieldset>
</form>
EOD;
?>
</body>
</html>
The Little Guy
10-10-2005, 09:48 PM
here is the code that I have, Is it basically the same thing as yours NogDog? Or would you recomend some other way to do this for me.
<HTML>
<HEAD>
<STYLE TYPE="text/css">
body{
font-size: 11px;
font-family: Verdana, Arial, Helvetica, sans-serif
}
#center
{
position:absolute;
width:300px;
top:30%;
left:35%;
text-align:center;
border: solid 1px #000000;
padding:5px;
}
h1{
position:absolute;
letter-spacing: 0.5cm;
left:0px;
top:20%;
width:99%;
font-size:20px;
font-weight:bold;
text-align:center;
padding:5px;
}
</STYLE>
<TITLE>Clue 1</TITLE>
</HEAD>
<HTML>
<BODY>
<h1>Clue 1</h1>
<?php
$errors = array();
if(isset($_REQUEST["seen_already"])){
validate_data();
if(count($errors) != 0){;
display_errors();
display_welcome();
}
else {
process_data();
}
}
else {
display_welcome();
}
function validate_data()
{
global $errors;
if(!preg_match('/soda/i', $_REQUEST["text"])){
$errors[] = "<font color='#FF0000'>Sorry Incorrect</font>";
}
}
function display_errors()
{
global $errors;
foreach ($errors as $err){
echo $err, "<br>";
}
}
function process_data()
{
echo "You Said: ";
echo $_REQUEST["text"];
}
function display_welcome()
{
echo "<form method='post action='soda.php'>";
echo "I am acid, yet sweet and tasty, I bring pain in the gums and money for the dentists, yet hapiness to the mouth.";
echo "<br>";
echo "<input name='text' type='text'>";
echo "<br>";
echo "<br>";
echo "<input type='submit' value='submit'>";
echo "<input type='hidden' name='seen_already' value='hidden_data'>";
echo "</form>";
?>
</BODY>
</HTML>
I have been working on this for 2-3 days, and I found this code in a book, but it gives me errors about this: </HTML> I dont know what to do, so I can't test this.
The Little Guy
10-10-2005, 10:00 PM
forget what I just said, I like your way, but I dont want there to be a next and previous link, so How would I remove that? When they press submit I want it to just automatically go to the next question, so how would I do that?
SpectreReturns
10-10-2005, 11:40 PM
@NogDog: You might think of putting in a similar_text check if it's greater than 90%, then continue, because some people have horrible spelling (not me, because I'm uber).
NogDog
10-11-2005, 06:04 AM
Take 2:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Quiz</title>
<style type="text/css">
fieldset {
width: 20em;
margin: 0 auto;
}
#result {
width: 20em;
margin: 0.5em auto;
color: #cc3333;
text-align: center;
}
</style>
</head>
<body>
<?php
# quiz data:
$data = array(
1 => array(
'question' => 'What is your favorite color?',
'answer' => 'Blue'),
2 => array(
'question' => 'What is your quest?',
'answer' => 'Holy Grail'),
3 => array(
'question' => 'What is the average unladen airspeed of a swallow?',
'answer' => 'African or European?'));
# set curret question number:
$nbr = (isset($_GET['nbr']) and array_key_exists( $_GET['nbr'], $data)) ?
$_GET['nbr'] : 0;
# if answer was given, show result:
echo "<p id='result'>";
if(isset($_POST['answer']))
{
$x = similar_text(strtolower($_POST['answer']),
strtolower($data[$nbr]['answer']),
$percent);
$result = ($percent >= 85) ? "Correct!" : # adjust percentage as needed
"Incorrect. The correct answer is '{$data[$nbr]['answer']}'";
echo "$result";
}
echo " </p>\n";
# display the form:
$next = $nbr + 1;
if(array_key_exists($next, $data))
{
echo <<<EOD
<form action='{$_SERVER['PHP_SELF']}?nbr=$next' method='post'>
<fieldset>
<legend>Question #$next</legend>
<p>{$data[$next]['question']}</p>
<p style='text-align: center;'><input type='text' name='answer'></p>
<p style='text-align: center;'><input type='submit' value='Submit'></p>
EOD;
echo "</fieldset>\n</form>\n";
}
else
{
echo "<p style='text-align: center'>Quiz completed.<br>".
"Link to somewhere else goes here</p>\n";
}
?>
</body>
</html>
The Little Guy
10-11-2005, 07:39 AM
When I run the second one I get this error:
Parse error: parse error in /home/www/ryanad.coolinc.info/hunter/test.php on line 68
The Little Guy
10-11-2005, 08:09 AM
nvm about what I just said, but I have one more request, if you dont mind. When they get the answer wrong, I dont want It to go to the next question, but stay at the same question, until it is right, then when it is right, it goes to the next question.
The Little Guy
10-11-2005, 09:35 AM
I fixed it, so I dont need another post, thanks for all the help, but here is what I have done:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Quiz</title>
<style type="text/css">
fieldset {
width: 20em;
margin: 0 auto;
}
#result {
margin: 0.5em;
padding: 0.5em;
border: solid 1px black;
color: #FF0000;
text-align: center;
background-color: #F2F2F2;
}
</style>
</head>
<body>
<?php
# quiz data:
$data = array(
1 => array(
'question' => 'What is your favorite color?',
'answer' => 'Blue'),
2 => array(
'question' => 'What is your quest?',
'answer' => 'Holy Grail'),
3 => array(
'question' => 'What is the average unladen airspeed of a swallow?',
'answer' => 'African or European?'));
# set current question number:
#$nbr = (isset($_GET['nbr']) and array_key_exists( $_GET['nbr'], $data)) ?
# : 0;
if (isset($_GET['nbr']) && array_key_exists( $_GET['nbr'], $data))
{
$nbr=$_GET['nbr'];
}
else
{
$nbr=1;
}
EOD;
if(isset($_POST['answer']))
{
if (strtolower($_POST['answer']) == strtolower($data[$nbr]['answer']))
{
$nbr = $nbr + 1;
$result="Correct!";
}
else
{
$result="Sorry, wrong answer.";
}
}
# display the form:
$next = $nbr;
if(array_key_exists($next, $data))
{
echo <<<EOD
<form action='{$_SERVER['PHP_SELF']}?nbr=$next' method='post'>
<fieldset>
<legend>Question #$next</legend>
<p>{$data[$next]['question']}</p>
<p style='text-align: center;'><input type='text' name='answer'></p>
<p style='text-align: center;'><input type='submit' value='Submit'></p>
</p>
EOD;
echo "<p id='result'>$result</p>";
echo "</fieldset>\n</form>\n";
}
else
{
echo "<p style='text-align: center'>You Got All The Clues.<br>".
"<a href=http://dumbells.no-ip.org>Home</a><br>".
"<a href=http://ryanad.coolinc.info/hunter/cluehunter.php>Start Over</a></p>\n";
}
?>
</body>
</html>