Header doesn't redirect after form submitted

I can’t get this form to re-direct after submission. Everything is fired into the database perfect but it just hangs on the page instead of redirecting.
I get the Header already modified error but I have removed all blank lines and (sadly) all the commented lines before the header. That normally works but in this case not.
I’ve run out of ideas. Can anyone else help?

Here’s what I have

<?php if (isset ($_GET[‘startnew’]))
{unset($_SESSION[‘majorregion’]);
unset($_SESSION[‘localarea’]);}
if (isset($_GET[‘majorregion’]))
{$_SESSION[‘majorregion’] = $_GET[‘majorregion’];}
if (isset ($_GET[‘localarea’]))
{$_SESSION[‘localarea’] = $_GET[‘localarea’];}
if (!function_exists(“GetSQLValueString”)) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = “”, $theNotDefinedValue = “”)
{if (PHP_VERSION < 6) {$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;}
$theValue = function_exists(“mysql_real_escape_string”) ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case “text”:
$theValue = ($theValue != “”) ? “'” . $theValue . “'” : “NULL”;
break;
case “long”:
case “int”:
$theValue = ($theValue != “”) ? intval($theValue) : “NULL”;
break;
case “double”:
$theValue = ($theValue != “”) ? doubleval($theValue) : “NULL”;
break;
case “date”:
$theValue = ($theValue != “”) ? “'” . $theValue . “'” : “NULL”;
break;
case “defined”:
$theValue = ($theValue != “”) ? $theDefinedValue : $theNotDefinedValue;
break;}
return $theValue;}}
$editFormAction = $_SERVER[‘PHP_SELF’];
if (isset($_SERVER[‘QUERY_STRING’])) {
$editFormAction .= “?” . htmlentities($_SERVER[‘QUERY_STRING’]);}
if ((isset($_POST[“MM_insert”])) && ($_POST[“MM_insert”] == “fr_lv3_interest”)) {
$insertSQL = sprintf(“INSERT INTO levelthreeinterest (core, white water, canoe, sea, surf, freestyle, slalom, WWR, touring, polo, racing, localarea, name, email, majorregion) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)”,
GetSQLValueString($_POST[‘core’], “int”),
GetSQLValueString($_POST[‘ww’], “int”),
GetSQLValueString($_POST[‘canoe’], “int”),
GetSQLValueString($_POST[‘sea’], “int”),
GetSQLValueString($_POST[‘surf’], “int”),
GetSQLValueString($_POST[‘freestyle’], “int”),
GetSQLValueString($_POST[‘slalom’], “int”),
GetSQLValueString($_POST[‘wwr’], “int”),
GetSQLValueString($_POST[‘touring’], “int”),
GetSQLValueString($_POST[‘polo’], “int”),
GetSQLValueString($_POST[‘racing’], “int”),
GetSQLValueString($_POST[‘localarea’], “text”),
GetSQLValueString($_POST[‘name’], “text”),
GetSQLValueString($_POST[‘email’], “text”),
GetSQLValueString($_POST[‘majorregion’], “text”));
mysql_select_db($database_con_pyb, $con_pyb);
$Result1 = mysql_query($insertSQL, $con_pyb) or die(mysql_error());
$insertGoTo = “courses-bcu-level3-confirm.php”;
if (isset($_SERVER[‘QUERY_STRING’])) {
$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
$insertGoTo .= $_SERVER[‘QUERY_STRING’];}
header(sprintf(“Location: %s”, $insertGoTo));}

You’ll need to do some debugging. Try checking to see if your POST variables are all set like you expect them to be.
I’ve added such a check near the end of the script.

If you don’t see the contents of your post, then your code isn’t getting to the bit that says:
if ((isset($_POST[“MM_insert”])) && ($_POST[“MM_insert”] == “fr_lv3_interest”)) {


if (isset ($_GET['startnew']))
{
    unset($_SESSION['majorregion']);
    unset($_SESSION['localarea']);
}

if (isset($_GET['majorregion']))
{
    $_SESSION['majorregion'] = $_GET['majorregion'];
}

if (isset ($_GET['localarea']))
{
    $_SESSION['localarea'] = $_GET['localarea'];
}

if (!function_exists("GetSQLValueString")) {

    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        if (PHP_VERSION &lt; 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
        switch ($theType) {

            case "text":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
            case "long":
            case "int":
            $theValue = ($theValue != "") ? intval($theValue) : "NULL";
            break;
            case "double":
            $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
            break;
            case "date":
            $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
            break;
            case "defined":
            $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
            break;
        }

        return $theValue;
    }

}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {

    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "fr_lv3_interest")) {
    // add this:
    var_dump($_POST);
    die();
    $insertSQL = sprintf("INSERT INTO levelthreeinterest (core, `white water`, canoe, sea, surf, freestyle, slalom, WWR, touring, polo, racing, localarea, name, email, majorregion) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
    GetSQLValueString($_POST['core'], "int"),
    GetSQLValueString($_POST['ww'], "int"),
    GetSQLValueString($_POST['canoe'], "int"),
    GetSQLValueString($_POST['sea'], "int"),
    GetSQLValueString($_POST['surf'], "int"),
    GetSQLValueString($_POST['freestyle'], "int"),
    GetSQLValueString($_POST['slalom'], "int"),
    GetSQLValueString($_POST['wwr'], "int"),
    GetSQLValueString($_POST['touring'], "int"),
    GetSQLValueString($_POST['polo'], "int"),
    GetSQLValueString($_POST['racing'], "int"),
    GetSQLValueString($_POST['localarea'], "text"),
    GetSQLValueString($_POST['name'], "text"),
    GetSQLValueString($_POST['email'], "text"),
    GetSQLValueString($_POST['majorregion'], "text"));
    mysql_select_db($database_con_pyb, $con_pyb);
    $Result1 = mysql_query($insertSQL, $con_pyb) or die(mysql_error());
    $insertGoTo = "courses-bcu-level3-confirm.php";
    if (isset($_SERVER['QUERY_STRING'])) {

        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
    }

    header(sprintf("Location: %s", $insertGoTo));
}

I get the Header already modified error but I have removed all blank lines and (sadly) all the commented lines before the header.

Commented and blank lines inside the php tags do not cause the error. It’s the blank lines and even spaces outside the php tags that cause it (and of course any output created by php or straight html code).

Check if there’s a space before the opening <?php tag.
Another cause may be the BOM. See this post for more info: http://www.sitepoint.com/forums/showthread.php?734957-Cannot-modify-header-information-headers-already-sent

Thanks for that. All looks good in the output:

array(17) { [“name”]=> string(10) “Bob Taylor” [“email”]=> string(17) “bobshouse@aol.com” [“majorregion”]=> string(7) “central” [“localarea”]=> string(4) “east” [“core”]=> string(1) “1” [“ww”]=> string(0) “” [“canoe”]=> string(0) “” [“sea”]=> string(1) “1” [“surf”]=> string(0) “” [“freestyle”]=> string(0) “” [“slalom”]=> string(0) “” [“wwr”]=> string(0) “” [“touring”]=> string(1) “1” [“polo”]=> string(1) “1” [“racing”]=> string(0) “” [“submit”]=> string(6) “Submit” [“MM_insert”]=> string(15) “fr_lv3_interest” }

Thanks for clearing that one up. At least when I fix it I will be able to go back and put all those useful comments back in for future reference.
However, I’ve deleted just about every space I can find - even in includes/javascript/jquery files etc.

I don’t quite know how I check the BOM but I do know that normally forms made in this editor do redirect so I guess it’s unlikely to be that.

Any other ideas?

I still can’t get it to redirect. I’ve take the spaces out of everything and everywhere. What else could it be?
Help?

I found out how to check the BOM an dI am not 100% sure that isn’t the problem.

Right then, next debug section!
Check if the header is going to the right place.


 $insertGoTo = "courses-bcu-level3-confirm.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
         
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
        $insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
     
    //header(sprintf("Location: %s", $insertGoTo));

echo $insertGoTo;


check that the URL is right and its going to the right place.
Taking the redirect off may also reveal any other errors that might be stopping it.

Thanks - I’ve done that.
The link echoed out is correct but it does have the regions in the address.

courses-bcu-level3-confirm.php?majorregion=north&localarea=northeast

Could that be causing the problem?

Just answered my own question by commenting out these lines:
//$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
//$insertGoTo .= $_SERVER[‘QUERY_STRING’];}

and running it. Still no redirection.

Does the page it is going to have any errors?
Is it getting redirected and then hanging or hanging on the submitted page - check the browser url

Also I would chuck an exit() call after the header to make sure nothing is still executed.


 $insertGoTo = "courses-bcu-level3-confirm.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
         
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
        $insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
     
    header(sprintf("Location: %s", $insertGoTo));
exit();

I added exit();
Now I just get ‘the white screen of death’ after submitting the form and still no redirect.
Is that what is supposed to happpen?

That means you are in the submitted page. Is it the same file submitted script page and redirecting one? As already Mike asked, I also suspect you have some warnings which causing to redirect. So try once by putting this on top of the script:


error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

if (isset ($_GET['startnew']))
{
	unset($_SESSION['majorregion']);
	unset($_SESSION['localarea']);
}
...........................

Okay,

That spits out this warning

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/mainsite/courses-bcu-level-three-register.php:2) in /Applications/MAMP/htdocs/mainsite/courses-bcu-level-three-register.php on line 61

But what does that tell me?

It tells you that at the start of courses-bcu-level-three-register.php there already is output (at or before line 2).
Is there anything in courses-bcu-level-three-register.php before the first <?php tag? A space, an empty line, html code, anything?

Have you included this courses-bcu-level-three-register.php file in other files or this is the file to which the form is submitted to? Check that parent file as well which has included this courses-bcu-level-three-register.php file.

Can’t see anything on line 1 or 2
The submits to itself a few times to create the query. Could that be the problem?

Here’s line 1 + line 2 anyway:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<?php require_once(‘Connections/con_pyb.php’);

So change it to:


<?php require_once('Connections/con_pyb.php');
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Done that.
Now I get this

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/mainsite/courses-bcu-level-three-register.php:3) in /Applications/MAMP/htdocs/mainsite/courses-bcu-level-three-register.php on line 59