KlappenVanJePa Posted March 6, 2013 Share Posted March 6, 2013 Hello, I have set up a form but when I fill it in it says the captcha math question is wrong. I have checked everthing but I can't find my problem. The strange thing that I get the error but It still sends a email. http://klappenvanjepa.co.nf/ <?php $num_one = rand() % 10; $num_two = rand() & 10; $final_num = $num_one + $num_two; $_SESSION['answer'] = $final_num; echo $num_one . ' + ' . $num_two . ' = '; ?> <input type="text" name="answer" /> <br /> <input type="submit" name="send_message" value="Send!"> $user_answer = $_POST['answer']; $real_answer = $_SESSION['answer']; if(empty($naam) OR empty($email) OR empty($email)) { echo "Your forget somthing."; } elseif($user_answer != $real_answer) { echo "Can't send because the question is wrong."; Can someone please help me with this problem. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/ Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 Do a var_dump on both answers. What are they? Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416979 Share on other sites More sharing options...
Psycho Posted March 6, 2013 Share Posted March 6, 2013 (edited) Are you using session_start() on both pages? EDIT: The strange thing that I get the error but It still sends a email. You don't show enough code to see that problem. All you have is this elseif($user_answer != $real_answer) { echo "Can't send because the question is wrong."; If you close out that execution block and have code later on in the page to send the email, then that error handling would do nothing to prevent that. You need to ensure the email logic is only run if there are no errors. Edited March 6, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416987 Share on other sites More sharing options...
KlappenVanJePa Posted March 6, 2013 Author Share Posted March 6, 2013 (edited) Do a var_dump on both answers. What are they? I am fairly new to php and I do not know what a var_dump is. I've looked on google but I can not clearly find it there. And also I looked in your "debbugging url but I still do not understand it". Can you please explain it otherwise. Thanks Edited March 6, 2013 by KlappenVanJePa Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416988 Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 var_dump. And yes, you need session_start() as the first line on each page. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416989 Share on other sites More sharing options...
KlappenVanJePa Posted March 6, 2013 Author Share Posted March 6, 2013 Are you using session_start() on both pages? Thanks very much, that was the problem. I'd did not expected the problem was so simple. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416990 Share on other sites More sharing options...
KlappenVanJePa Posted March 6, 2013 Author Share Posted March 6, 2013 Oke I thought the problem was finaly over because it says that it was sent, but now I don't get a email anymore. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416996 Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 You didn't post any code that sends an email. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416998 Share on other sites More sharing options...
Psycho Posted March 6, 2013 Share Posted March 6, 2013 Oke I thought the problem was finaly over because it says that it was sent, but now I don't get a email anymore. As I stated previously, you don't provide enough code to see where that problem may be. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1416999 Share on other sites More sharing options...
KlappenVanJePa Posted March 6, 2013 Author Share Posted March 6, 2013 Are you using session_start() on both pages? EDIT: You don't show enough code to see that problem. All you have is this elseif($user_answer != $real_answer) { echo "Can't send because the question is wrong."; If you close out that execution block and have code later on in the page to send the email, then that error handling would do nothing to prevent that. You need to ensure the email logic is only run if there are no errors. <?php session_start(); $email = $_POST['email']; $naam = $_POST['naam']; $onderwerp = $_POST['onderwerp']; $vragen = $_POST['vragen']; $user_answer = $_POST['answer']; $real_answer = $_SESSION['answer']; if(empty($naam) OR empty($email) OR empty($email)) { echo "Uw bent iets vergeten in te vullen."; } elseif($user_answer != $real_answer) { echo "can't send because the question is wrong."; } else { mail($to, $subject, $message, "From: $email"); echo "Message sent"; } $to = 'tymenw@Gmail.com'; $subject = 'contact'; $body = <<<EMAIL Naam: $naam Email: $email Onderwerp: $onderwerp vragen: $vragen EMAIL; $header = ('Location: index.html'); if($_POST){ if($naam == ''|| $email == ''|| $vragen == '' ){ $feedback = 'U forget something'; }else{ mail($to, $subject, $body, $header); $feedback = 'Thanks The message has been sended'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1417000 Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 So... you send the email before you define the variables that you use to send the email. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1417004 Share on other sites More sharing options...
KlappenVanJePa Posted March 6, 2013 Author Share Posted March 6, 2013 Yes, I guess so. That was pretty stupid of me I've now done and put it on the end of the page, but nothing changes it still don't works. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1417007 Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 Do you see a theme yet with your posts and ours? Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1417014 Share on other sites More sharing options...
Jessica Posted March 6, 2013 Share Posted March 6, 2013 1. $header = ('Location: index.html'); This is just way wrong. header() is a function. I doubt you want to create a string with "Location: index.html' stored in it. 2. you use $body and $message. Pick one. Quote Link to comment https://forums.phpfreaks.com/topic/275333-math-question-by-a-form-dont-work/#findComment-1417015 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.