Jump to content

[SOLVED] IF / ELSE function(); issue


tpsilver10

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

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.