Jump to content

can a redirect to a local page from within a function?


spryce

Recommended Posts

Newbie question.

 

I have a form that uses reCaptcha. If the Captcha is entered correctly it posts the data.

 

Can I redirect the page using php or do I need to use javascript?

 

I was going to throw in

header('Location: thankyou.php');

but from my limited understanding this cannot be used anywhere except the very top of the page.

 

  if (!$resp->is_valid) {
//    What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
  echo "Recapture entered correctly!"; // Just for testing purposes
	postdata($firstname, $lastname, $company, $email, $phone, $comments); // sends data to my databse

// this is where I would expect to redirect to a new page

  }

 

Thanks phpfreaks

 

 

PS - i did read the sticky on header errors.

 

I have a stack of $POST calls ie

$subject = check_input($_POST['subject']);

before my function. I assume this means that data is sent to the browser which is why I cant use the header function.

But the reCaptcha stuff doesnt work unless this comes first.  :(

If echo "Recapture entered correctly!"; isn't commented out, the redirect surely won't work after it. Other than that, it's impossible to tell without the rest of the preceding code. Also, whenever you use a header() redirect, you should immediately follow it with an exit(); to stop the further execution of code in the script.

$subject = check_input($_POST['subject']);

 

You can have a header() call after that... $_POST is a global array of data the user submitted to the script.  You can have a header() call anywhere in your script, as long as there is no output before it (and even then you can, though it's not recommended)

Archived

This topic is now archived and is closed to further replies.

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