I having trouble with my re-direct code on my php registration form. The form does submit data to the database but I'm getting the following error.
"Warning: Cannot modify header information - headers already sent by (output started at /homepages/25/d232402382/htdocs/testencourage/registration.php:15) in /homepages/25/d232402382/htdocs/testencourage/registration.php on line 87"
I would like the form goes to the thanks.php after submitting the data but its not doing its job. Here is the code.
<?php
if ($_SERVER['HTTPS']) {
header('location: https://www.fakewebsite.com');
}
the regular html syntax <title>, site navigation, etc...
?>
<?php
if (isset($_POST['submitted'])){
$fields = array(
'email',
'state',
'district',
'gender',
'age',
'profession',
);
if (safe($_POST['survey']=="Yes")){
$survey = "Yes";
}
else{
$survey = "No";
}
foreach($fields as $fieldName) {
if(isset($_POST[$fieldName]) and safe(trim(stripslashes($_POST[$fieldName]))) !==''){
$$fieldName = safe(trim(stripslashes($_POST[$fieldName])));
}else {
$errors[] = "Please enter your". $fieldName .""; //code to validate fields
}
}
if(!isset($errors)){
require_once('Connections/encourage.php');
$query = "INSERT INTO participants (email, state, district, gender, age, profession, survey, registration_date)
VALUES ('$email', '$state', '$district', '$gender', '$age', '$profession','$survey', NOW())"; //databasse connection
$result = mysql_query ($query);
if ($result){
$url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') || (substr($url, -1) == '\\')) {
$url = substr ($url, 0 -1);
}
$url .= '/thanks.php';
header("Location: $url");// this is line 87
exit();
}else{
echo '<h1 id="mainhead">System Error</hl>
<p>Your registration could not be completed due to a system error We apologize for any incovience</p>';//gives system error
echo 'p' . mysql_error(). '<br /><br />Query: ' . $query . '</p>';
exit();
}
mysql_close();
} else {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach($errors as $msg) {
echo " - $msg<br/>\n";
}
echo '</p><p>Please try again.</p><p><br/></p>';
}
}
function safe($string)
{
$pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";
return preg_replace($pattern, '', $string);
}
?>
Also I'm going to set up my ssl on the server so I'm planning to use that https redirect syntax but since I'm very new at this, am I missing something. Your help will be greatly appreciated, thanks!