I have made a basic messaging system with Php & I'm wondering why I get this error message:
Warning: Cannot modify header information - headers already sent on line 33
Here's my full code:
<?php
if (isset($_GET['hash'])&&!empty($_GET['hash'])){
$hash = mysqli_escape_string($con, $_GET['hash']);
$message_query = "SELECT from_id, message FROM messages WHERE group_hash='$hash'";
$run_messages = mysqli_query($con,$message_query);
while($row_messages = mysqli_fetch_array($run_messages, MYSQLI_ASSOC)){
$from_id = $row_messages['from_id'];
$message = $row_messages['message'];
$user_query = "SELECT username FROM admins WHERE id='$from_id'";
$query_run = mysqli_query($con, $user_query);
$run_user = mysqli_fetch_array($query_run, MYSQLI_ASSOC);
$from_username = $run_user['username'];
echo "
<div class='widgetbox'>
<h4 class='widgettitle'><i>$from_username</i></h4>
<div class='widgetcontent'>
$message
</div>
</div>
";
}
?>
<div class='widgetbox'>
<form method='POST' action=''>
<?php
if (isset($_POST['message'])&&!empty($_POST['message'])){
$new_message = $_POST['message'];
$insert_reply = "INSERT INTO messages VALUES('','$hash','$id','$new_message')";
$run_reply = mysqli_query($con,$insert_reply);
header('Location: conversations.php?hash='.$hash);
}
?>
<h4 class='widgettitle'>Enter Your Reply <a class='close'>×</a> <a class='minimize'>–</a></h4>
<div class='widgetcontent'>
<textarea name='message' rows='6' cols='50'></textarea></br>
<button name='submit' type='submit' class='btn btn-primary'>Send Message</button>
</div>
</br></br>
</form>
</div>
<?php
}else{
header('Location: dashboard.php');
}
?>
Here's line 33:
header('Location: conversations.php?hash='.$hash);
I searched alot about this error message & I found 2 useful answers:
1- header
must be at the top of your page but as you can I must place it right there because it's a part of my action scripts for my form.
2- Using Javascript header tag but that's not a good idea cause I need to process this link via Php: conversations.php?hash='.$hash
Therefore I have to fix this header problem. Please if you know how can I be do that , let me know ... I really appreciate! Thanks :)