Jump to content

Need help with simple form..


phpnoobie9

Recommended Posts

Trying to make a simple captcha from simple math questions.

Can't seem to echo $question in the form if I don't take the if (isset post submit) part out.

If I do take out the isset and view the page I automatically get else echo 'Wrong' displayed, but if I fill in the correct answer it'll say yeah! like it should. Just wondering why it automatically shows else.

 

I eventually want to add this to my form.

 

<?php

$captchaquery = "SELECT * FROM captcha ORDER BY RAND() LIMIT 1";
if (isset ($_POST['submit'])) {

if ($captcharesult = mysql_query ($captchaquery)) {
while ($captcharow = mysql_fetch_array ($captcharesult)) {
$question = $captcharow['questions'];
$answers = $captcharow['answers'];
$answer = $_POST['answer'];
}
if ($answers == $answer) {
echo 'Yeah';
}
else {
echo 'Wrong';
} 
}

}

?>
<form action="test.php" method="post">
<?php echo $question  ?> <input type="text" name="answer" size="1" maxlength="2" />
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/98673-need-help-with-simple-form/
Share on other sites

try it this way

<?php
// first of all check to see if you are answering the question
// if its not set then write out a question
if(!isset($_POST['submit']))
{
$captchaquery = "SELECT * FROM captcha ORDER BY RAND() LIMIT 1";
$captcharesult = mysql_query ($captchaquery);
while ($captcharow = mysql_fetch_array ($captcharesult)) {
$question = $captcharow['questions'];
$answers = $captcharow['answers'];	
}
}
// if the post variable is set then check for the correct answer
if (isset ($_POST['submit'])) 
{
$answer = $_POST['answer'];
if ($answers == $answer) {
echo 'Yeah';
}
else {
echo 'Wrong';
} 
}

?>
<form action="test.php" method="post">
<?php echo $question  ?> <input type="text" name="answer" size="1" maxlength="2" />
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

<?php

// database connection....

$sql = "SELECT * FROM captcha ORDER BY RAND() LIMIT 1";
$sql_result=mysql_query($sql)or die(mysql_error);

while($row=mysql_fetch_assoc($sql_result)){

if (isset($_POST['submit'])) {

    $answer = $_POST['answer'];

if ($answer == $row['ansaw']) {

	echo 'Yeah';

}else {

	echo 'Wrong';
} 
}

}

?>
<form action="test.php" method="POST">
<?php echo $row['question'];  ?> <input type="text" name="answer" size="1" maxlength="2" />
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

try it this way

<?php
// first of all check to see if you are answering the question
// if its not set then write out a question
if(!isset($_POST['submit']))
{
$captchaquery = "SELECT * FROM captcha ORDER BY RAND() LIMIT 1";
$captcharesult = mysql_query ($captchaquery);
while ($captcharow = mysql_fetch_array ($captcharesult)) {
$question = $captcharow['questions'];
$answers = $captcharow['answers'];	
}
}
// if the post variable is set then check for the correct answer
if (isset ($_POST['submit'])) 
{
$answer = $_POST['answer'];
if ($answers == $answer) {
echo 'Yeah';
}
else {
echo 'Wrong';
} 
}

?>
<form action="test.php" method="post">
<?php echo $question  ?> <input type="text" name="answer" size="1" maxlength="2" />
<br />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

 

Can't seem to get it to show $question in the form section after submission correct or wrong answer.

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.