StumpClassic
StumpClassic

Reputation: 3

PHP header not redirecting when using forms

I'm doing form validation using a PHP script. I initially wrote this code in 2007 but now it just stopped working, and I've been trying to figure out why.

Here's the code:

<?php
$error_msg = '';

// Only Validate Form when it is submitted
if (isset($formSubmit)) {
   if (!isset($_SESSION["First_Name"])) {
     $get_mbr_id = urlencode ($_POST["GetMbrID"]);
     $_SESSION["MemberID"] = $get_mbr_id;
     }

   if (!headers_sent()) {
     header ("Location: mywebsite.com");
     exit (0);
     }
}

if (isset($formExit)) {
  if (!headers_sent()) {
    header ('Location: mywebsiteexit.com');
    exit (0);
    }
}
?>
<html><head></head><body>
<form name="select_action" method="POST" action="select_action">
<br>
<center>
<input type="submit" name="formSubmit" value="Next">
<input type="reset" name="fieldReset" value="Reset">
<input type="submit" name="formExit" value="Cancel">
</center>
</form></body></html>

If the HTML form code is present, then the header redirect doesn't work.

However, if I remove the HTML form code, change the if(isset(formSubmit)) statement to if(!isset(formSubmit)), then the header redirect will work.

I can't figure what is happening with the form code that causes the header() redirect not to happen.

Any help would be appreciated!

Upvotes: 0

Views: 903

Answers (2)

Elitmiar
Elitmiar

Reputation: 36839

Make 100% dure there isn't a blank line at the top before the

Upvotes: 0

erenon
erenon

Reputation: 19118

You have to check for posts data in the $_POST superglobal. Register_globals has turned off.

if (isset($_POST['formSubmit'])) {
//etc.

Upvotes: 3

Related Questions