tpsilver10 Posted May 9, 2007 Share Posted May 9, 2007 I'm trying to get this CAPTCHA field to work in my site. This is the code I have at the moment, which will tell me I have the wrong code. It will also tell me I have the right code is I replace 'letsgo();' with 'echo "yay"'. So I'm assuming there is something wrong with the else { letsgo(); }...I just cannot figure it out and its driving me made. <?php if( isset($_POST['submit'])) { if( $_SESSION['security_code'] != $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { echo 'Wrong Code'; unset($_SESSION['security_code']); } else { letsgo(); } } else { function letsgo() { // check we're receiving something if (empty($_POST)) ft_output_message("no_post_vars"); // check there's a form ID included else if (empty($_POST['form_tools_form_id'])) ft_output_message("no_form_id"); // is this an initialization submission? else if (isset($_POST['form_tools_initialize_form'])) ft_initialize_form(); // otherwise, it's a regular form submission. Process it. else process_form(); } //rest of the form ?> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/ Share on other sites More sharing options...
trq Posted May 9, 2007 Share Posted May 9, 2007 Pardon? Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249127 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Didn't we already discuss this. Your function definition has to be outside of the else statement or else that function is never defined. You may want to review SCOPE. Here is an example: <?php $i=1; if ($i == 0) { function testMe() { print "hello"; } }else { function testMe() { print "goodbye"; } } testMe(); ?> Or maybe this: <?php $i=1; if ($i == 0) { function testMe() { print "hello"; } }else { function testMine() { print "goodbye"; } } testMine(); // should print "goodbye" testMe(); // should throw a fatal error call to undefined function. ?> Syntax and placement can mean everything to a script. Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249133 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 Okay... I'm trying to get this CAPTCHA field to check whether the security code was entered correctly. If it wasn't, then the user gets told so - this bit works so far no problems. If the right code was entered, then nothing happens - the else part. It doesn't perform the letsgo() function. However, if I replace letsgo(); with echo 'It works'; then it tells me it works fine. So I'm assuming the problem lies within the calling and performing of the function letsgo() Is that clear or am I still confusing myself and everyone else... Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249135 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 We did discuss it...but unless I went wrong somewhere, the same thing happened (ie nothing). Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249138 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Wow, seriously read what people post. <?php function letsgo() { // check we're receiving something if (empty($_POST)) ft_output_message("no_post_vars"); // check there's a form ID included else if (empty($_POST['form_tools_form_id'])) ft_output_message("no_form_id"); // is this an initialization submission? else if (isset($_POST['form_tools_initialize_form'])) ft_initialize_form(); // otherwise, it's a regular form submission. Process it. else process_form(); } if( isset($_POST['submit'])) { if( $_SESSION['security_code'] != $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { echo 'Wrong Code'; unset($_SESSION['security_code']); } else { letsgo(); } } else { //rest of the form } ?> That should work since the DEFINITION of the FUNCTION is now OUTSIDE the ELSE statement so it CAN be USED THROUGHOUT the whole SCRIPT and not JUST if that ELSE statement was TRUE. Understand? Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249141 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 No need to get pissed of champ... And like I said, it doesn't work. Give it a whirl: http://www.pacesettertravel.com/pacesetter/index.php?id=quotes&subid=downunder Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249143 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 That does not show me anything, I cannot see the code underlayed in the index.php to know that you did make the necessary changes. <?php function letsgo() { // check we're receiving something if (empty($_POST)) { ft_output_message("no_post_vars"); }elseif (empty($_POST['form_tools_form_id'])) { ft_output_message("no_form_id"); }elseif (isset($_POST['form_tools_initialize_form'])) { ft_initialize_form(); }else { process_form(); } } if( isset($_POST['submit'])) { if( $_SESSION['security_code'] != $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) { echo 'Wrong Code'; unset($_SESSION['security_code']); } else { letsgo(); } } else { //rest of the form } ?> I bet part of the problem was the elseif, which we did discuss in the earlier thread. The other part was the scope of the letsgo. So lets create a debugging portion <?php function letsgo() { // check we're receiving something $_POST['test'] = "something"; $_POST['form_tools_form_id'] = 1; if (empty($_POST)) { print "no post vars"; }elseif (empty($_POST['form_tools_form_id'])) { print "tools form id"; }elseif (isset($_POST['form_tools_initialize_form'])) { print "form Initilization"; }else { print "my form processed just fine"; } } $x=1; $b=2; if ($x == 1) { if ($b != 2) { echo 'Wrong Code'; } else { echo 'Lets Go!'; letsgo(); } } else { print "we are here"; //rest of the form } ?> When that executes it prints Lets Go! my form processed just fine. A plain and simple example and it works, there should be no reason why it would not work on your side unless you have errors supressed and the functions such as processform(); are not defined somewhere. Not pissed off, getting very frustrated that you do not seem to listen to what is being said as you so eagerly went back to the original code which is definately flawed one way or the other and you knew that but still decided to stick with it. Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249155 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 Fair enough. I wasn't ignoring what you said, I tried it out and it didn't work. I went back to the original code because I knew that the echo telling me the security code was right worked. Anyway, I've tried that out and it works fine over here. Now I guess it's just a matter of making it work as I need it to... Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249170 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 Okay...so this is what I have. <?php function letsgo() { $_POST['test'] = "something"; $_POST['form_tools_form_id'] = 1; if (empty($_POST)){ print "no post vars"; } elseif (empty($_POST['form_tools_form_id'])){ print "tools form id"; } elseif (isset($_POST['form_tools_initialize_form'])){ print "form Initilization"; } else process_form(); } if (isset($_POST['submit'])) { if ($_SESSION['security_code'] != $_POST['security_code'] && !empty($_SESSION['security_code'])){ echo 'Wrong Code'; unset($_SESSION['security_code']); } else { echo 'Lets Go!'; letsgo(); } } else { //rest of the form ?> It prints 'Lets Go!' no problems...still doesn't execute the function. And I add print 'we are here'; to the last else {, not even the 'Lets Go!' is printed. Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249177 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Is error reporting turned on? The last else that does the process_form needs the { } if the rest of the if has braces, that should throw a syntax error of sometype. More debugging is needed: <?php function letsgo() { echo 'letsgo was initiated<br />'; $_POST['test'] = "something"; $_POST['form_tools_form_id'] = 1; if (empty($_POST)){ print "no post vars"; } elseif (empty($_POST['form_tools_form_id'])){ print "tools form id"; } elseif (isset($_POST['form_tools_initialize_form'])){ print "form Initilization"; } else{ echo 'Process the form <br />'; process_form(); } } if (isset($_POST['submit'])) { echo 'submit was set <br/>'; if ($_SESSION['security_code'] != $_POST['security_code'] && !empty($_SESSION['security_code'])){ echo 'Wrong Code<br />'; unset($_SESSION['security_code']); } else { echo 'Lets Go!<br />'; letsgo(); } } else { echo 'submit was not set <br />'; //rest of the form ?> See where that gets ya. Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249187 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 Well that's doing as it should...but still nothing gets e-mailed/added to the database...grrr. submit was set Lets Go! letsgo was initiated Process the form I'm still sure there's just some problem with letsgo() and process_form()... Appreciate the help mate. Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249194 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 Where is the code for process_form() ?? It seems that the code above is working flawlessly as everything was printed like expected right? So the problem lies within the process_form() code. Where is it defined and where is the code? Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249196 Share on other sites More sharing options...
tpsilver10 Posted May 9, 2007 Author Share Posted May 9, 2007 AHA! Okay...I have it submitting the things now. Basically...duhhh...I put this code that you've giving me BELOW the process_form() function and voila. I guess that makes sense with the declaration before execution. Now there's just a little bit to fiddle with... Cheers mate! Quote Link to comment https://forums.phpfreaks.com/topic/50680-solved-if-else-function-issue/#findComment-249200 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.