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
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>

Link to comment
Share on other sites

<?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>

Link to comment
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>

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.