phpnoobie9 Posted March 30, 2008 Share Posted March 30, 2008 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> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted March 30, 2008 Share Posted March 30, 2008 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> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 30, 2008 Share Posted March 30, 2008 <?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> Quote Link to comment Share on other sites More sharing options...
phpnoobie9 Posted March 30, 2008 Author Share Posted March 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.