Header Location Question

Hi everyone,

Is there any way this code could be rewritten to have the header function at the top of the page?
At the minute I get the dreaded “Warning: cannot modify header information”. It targets two things.
Output Started on line 15 (the first opening php tag) and the header function on line 47.

[php]

Get in Touch | Contact Us ...

Get in touch

Comments / Feedback / Interesting Ideas.


<?php //connect to the database $dbc = mysqli_connect('host', 'user', 'password', 'dbname') or die('Error connecting to MySQL Server.'); //process the form if(isset($_POST['submit'])){ $name = mysqli_real_escape_string($dbc, trim($_POST['name'])); $email = mysqli_real_escape_string($dbc, trim($_POST['email'])); $comment = mysqli_real_escape_string($dbc, trim($_POST['comment'])); $name_error = ""; $email_error = ""; $comment_error = ""; $output_form = false; //Simple Validation + Required in form if(empty($name)){ echo '

* Please Add a Name.

'; $name_error = "color: #C00; font-weight: bold;"; $output_form = true;} if(empty($email)){ echo '

* Please Add an Email.

'; $email_error = "color: #C00; font-weight: bold;"; $output_form = true;} if(empty($comment)){ echo '

* Please Add Some Comments.

'; $comment_error = "color: #C00; font-weight: bold;"; $output_form = true;} if ((!empty($name)) && (!empty($email)) && (!empty($comment)){ header ('Location: success.php');} //create the insert statement $query = "INSERT INTO tblname(name, email, comment) VALUES ('$name', '$email', '$comment')"; //insert record into the table $result = mysqli_query($dbc, $query) or die ('Error querying database.'); //close connection to database mysqli_close($dbc); } else{ $output_form = true;} if ($output_form){ ?>
    <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
		
		<label style="<?php echo isset($name_error) ? $name_error : ""; ?>"><span class="asterisk">*</span>Name</label>
		<input name="name" type="text" value="<?php if(isset($_POST['name'])) echo htmlentities($_POST['name']);?>" required autofocus autocomplete="on">
		
		
		<label style="<?php echo isset($email_error) ? $email_error : ""; ?>"><span class="asterisk">*</span>Email</label>
		<input name="email" type="email" value="<?php if(isset($_POST['email'])) echo htmlentities($_POST['email']);?>" required autocomplete="on">
		
		
		<label style="<?php echo isset($comment_error) ? $comment_error : ""; ?>"><span class="asterisk">*</span>Comments</label>
		<textarea name="comment" type="text" required> 
		<?php if(isset($_POST['comment'])) echo htmlentities($_POST['comment']);?></textarea><br>
		<br>
		<input id="submit" name="submit" type="submit" value="Send">
	</form>
	<?php } ?>
    <br>
		</div>...
[/php]

Some things I have tried in place of the header:

echo ‘’;
–issue: causes a slight delay / jump in page before success.php loads.

printf(“”);
–issue: same as meta refresh, slight delay / jump in page before success.php loads.

Ideally, I would prefer to use the line header(‘Location: success.php’) if possible.

Thanks for any help,

Andy :wink:

You cannot modify the header info (location, cookies, etc) after output has started. Your output starts the moment you echo/print/dump something from PHP, or a PHP file contains anything outside of PHP tags (like yours which start with HTML).

This is one of the reasons it’s recommended to arrange your files like this

[php]<?php

// all logic here, db/api calls, manipulation of data, form error handling, database updates, etc
$data = $db->query(’…’);

?>

View

<?= $data['name'] ?>

[/php]

Hi JimL,

thanks for the reply. I tried that too (all php content at the top), it didn’t work, I still get the same error. I realise that the header has to be the first thing, no spaces, gaps etc. like so:

[php]<?php
header(‘Location:success.php’);
?>[/php]

but how can I have it at the top but still refer to it on line 47, so it fires at the correct time?
Perhaps, it is not a header(‘Location: success.php’), I should be trying to do, but instead the meta refresh or JS alternative.

Thanks, Andy :wink:

Also, remember that nothing executes after a change of location… So, normally, you process everything up to the page move
before the HTML page. That way if everything is good to go, then you process the saving of data or sending emails or what is
needed, then you header-location to another page. If the data is not accurate then, the form simply re-displays with the data
posted in place to allow the user to fix errors.

Thanks for the suggestions JimL, ErnieAlex.

I think I figured it out. Just looked at the settings in my editor - Notepad++.
The file is saved as UTF-8 + BOM, which must have been sending extra characters before the header.

Thanks again for the help,

Andy :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service