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