jeff5656 Posted February 24, 2009 Share Posted February 24, 2009 When my php code arrives at this line: <?php error_reporting(E_ALL); include "dateheader.php"; (SNIPPED CODE) header("Location: displayactive.php"); I get this error:Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\wb\dateheader.php:223 Here is dateheader.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xml:lang="en"> <head> <title>title page</title> <meta http-equiv="Content-Language" content="en-gb" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <style type="text/css"> etc. Which of these 3 files do I need to "fix" and how do I fix it? If you need me to display the displayactive.php file let me know. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/ Share on other sites More sharing options...
rhodesa Posted February 24, 2009 Share Posted February 24, 2009 header("Location: displayactive.php"); is basically a redirect. but, you can't do a redirect AFTER you have outputted info already. what are you trying to accomplish with that line of code? Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-770503 Share on other sites More sharing options...
jeff5656 Posted February 24, 2009 Author Share Posted February 24, 2009 I want to redirect to displayactive.php. here is that file: <?php require('secure.php'); include "connectdb.php"; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Active Patients - Staff View</title> <SCRIPT TYPE="text/javascript"> etc. Both the dateheader.php and displayactive.php have headers, but those headers are needed, so do I need to delete all the info in the header of one of these files?? Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-770510 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 You would put your PHP code above the output, outputting php as you go is not a "Great" practice. It is far better to output after the processing or store the output in a string and echo that one statement out at the end. A "bandaid" for this issue is ob_start but I would suggest fixing your code to flow properly. Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-770565 Share on other sites More sharing options...
jeff5656 Posted February 25, 2009 Author Share Posted February 25, 2009 you would put your PHP code above the output WHICH code? The code in displayactive.php or the file that has header("Location: displayactive.php"); By "output" I assume you are referring to things inside a <head> </head>? The file that has header ("location....) has a bunch of code before it that processes a form. I want to re-direct to "displayactive.php", so do I need to put my whole entire php code before the header in displayactive? That file is filled with php code mixed with html and I need that header info I think. Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-770914 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 The file that has header ("location....) has a bunch of code before it that processes a form. I want to re-direct to "displayactive.php", so do I need to put my whole entire php code before the header in displayactive? That file is filled with php code mixed with html and I need that header info I think. Mixing outputted html with php is not very good practice, especially if you need to output it before header calls. You should store output in a string and output it at the end of the script to avoid such issues. Here the header is it needs to be before any data is output to the screen. So right after <?php and the <?php needs to be at the very top of the script. You may have to re-work your code to get this to work, by either re-thinking the logic and figuring out a way to get that header redirect up top, or by storing output into a string then outputting at the end of the script. Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-770995 Share on other sites More sharing options...
jeff5656 Posted February 25, 2009 Author Share Posted February 25, 2009 Here is the very top of displayactive.php: <?php require('secure.php'); include "connectdb.php"; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Active Patients - Staff View</title> <SCRIPT TYPE="text/javascript"> <!-- As you can see, it starts with <?php, so how do I change displayactive.php to prevent the error and still redirect to this page? As you might have noticed, I am slow :-) given that we are up to message#5, I am not understanding *specifically* how to solve the problem, although I do understand your generic points about output. Now if there is another way besides this problematic "header" command to redirect to another page by using PHP, I'm all ears! Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771203 Share on other sites More sharing options...
jeff5656 Posted February 25, 2009 Author Share Posted February 25, 2009 "Mixing outputted html with php is not very good practice," Ok how would you do this without mixing: <td>hello your name is <?php echo $row['name'];?></td> So I (in my limited humble experience) take issue with not mixing html and php. It's done all the time! BUT I mainly want to figure out the current problem, so I hope I don't distract anyone with this message and then I don't get a solution - I just wanted to point this out :-) Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771204 Share on other sites More sharing options...
sastro Posted February 25, 2009 Share Posted February 25, 2009 You should do something like this <?php error_reporting(E_ALL); if(blablabla){ header("Location: displayactive.php"); exit; } include "dateheader.php"; (SNIPPED CODE) Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771211 Share on other sites More sharing options...
jeff5656 Posted February 25, 2009 Author Share Posted February 25, 2009 Hi, I tried that but it didn't work. Ok I'm going to show my whole code. Where in this code do I change it so I can redirect to displayactive.php after this code is run?? <?php error_reporting(E_ALL); require('secure.php'); include "dateheader.php"; include "connectdb.php"; $newdate = date("Y-m-d", strtotime($_POST['rcf_date'])); echo "newdate:" . $newdate . "<br>"; $current_date = date ("m/d/y"); $query = "SELECT * FROM `icu` WHERE `id_incr` = '".$_POST['id_incr']."' "; $result = mysql_fetch_array(mysql_query($query)) or die(mysql_error()); echo "<br> problist:" . $result['problist']; $problist = $result['problist']; if($problist != $_POST[problist]) { $problist_date = date("Y-m-d H:i:s"); } else { $problist_date = $result['problist_date']; } $todo = $result['todo']; if($todo != $_POST[todo]) { $todo_date = date("Y-m-d H:i:s"); } else { $todo_date = $result['todo_date']; } $sql = "UPDATE icu SET rm_loc = '" . mysql_real_escape_string($_POST['rm_loc']) . "', patient = '" . mysql_real_escape_string($_POST['patient']) . "', mrn = '" . mysql_real_escape_string($_POST['mrn']) . "', age = '" . mysql_real_escape_string($_POST['age']) . "', race = '" . mysql_real_escape_string($_POST['race']) . "', gender = '" . mysql_real_escape_string($_POST['gender']) . "', attg = '" . mysql_real_escape_string($_POST['attg']) . "', cs_doc = '" . mysql_real_escape_string($_POST['cs_doc']) . "', rcf_date = '" . $_POST['rcf_date'] . "', dx = '" . mysql_real_escape_string($_POST['dx']) . "', pmhx = '" . mysql_real_escape_string($_POST['pmhx']) . "', code = '" . mysql_real_escape_string($_POST['code']) . "', allergy = '" . mysql_real_escape_string($_POST['allergy']) . "', todo = '" . mysql_real_escape_string($_POST['todo']) . "', todo_date = '$todo_date', problist_date = '$problist_date', problist = '" . mysql_real_escape_string($_POST['problist']) . "', comments = '" . mysql_real_escape_string($_POST['comments']) . "', signoff_status = '" . $_POST['signoff_status'] . "', rcf_date2 = '$newdate' WHERE id_incr = ".$_POST['id_incr'].""; if (isset($sql) && !empty($sql)) { echo "<!--" . $sql . "-->"; $result = mysql_query($sql) or die ("Invalid query: " . mysql_error()); ?> <p> Done <?php } header("Location: displayactive.php"); ?> [code] Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771216 Share on other sites More sharing options...
sastro Posted February 25, 2009 Share Posted February 25, 2009 Dude,.. it should be no html code include space before header command. Just change <?php } header("Location: displayactive.php"); ?> with <script type="text/javascript"> <!-- window.location = "displayactive.php" //--> </script> Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771227 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 The javascript way is a halfassed way imo. <?php error_reporting(E_ALL); require('secure.php'); include "dateheader.php"; include "connectdb.php"; $newdate = date("Y-m-d", strtotime($_POST['rcf_date'])); // echo "newdate:" . $newdate . "<br>"; // commented out as it will cause header issues. $current_date = date ("m/d/y"); $query = "SELECT * FROM `icu` WHERE `id_incr` = '".$_POST['id_incr']."' "; $result = mysql_fetch_array(mysql_query($query)) or die(mysql_error()); // echo "<br> problist:" . $result['problist']; // commented out as it will cause header issues. $problist = $result['problist']; if($problist != $_POST[problist]) { $problist_date = date("Y-m-d H:i:s"); } else { $problist_date = $result['problist_date']; } $todo = $result['todo']; if($todo != $_POST[todo]) { $todo_date = date("Y-m-d H:i:s"); } else { $todo_date = $result['todo_date']; } $sql = "UPDATE icu SET rm_loc = '" . mysql_real_escape_string($_POST['rm_loc']) . "', patient = '" . mysql_real_escape_string($_POST['patient']) . "', mrn = '" . mysql_real_escape_string($_POST['mrn']) . "', age = '" . mysql_real_escape_string($_POST['age']) . "', race = '" . mysql_real_escape_string($_POST['race']) . "', gender = '" . mysql_real_escape_string($_POST['gender']) . "', attg = '" . mysql_real_escape_string($_POST['attg']) . "', cs_doc = '" . mysql_real_escape_string($_POST['cs_doc']) . "', rcf_date = '" . $_POST['rcf_date'] . "', dx = '" . mysql_real_escape_string($_POST['dx']) . "', pmhx = '" . mysql_real_escape_string($_POST['pmhx']) . "', code = '" . mysql_real_escape_string($_POST['code']) . "', allergy = '" . mysql_real_escape_string($_POST['allergy']) . "', todo = '" . mysql_real_escape_string($_POST['todo']) . "', todo_date = '$todo_date', problist_date = '$problist_date', problist = '" . mysql_real_escape_string($_POST['problist']) . "', comments = '" . mysql_real_escape_string($_POST['comments']) . "', signoff_status = '" . $_POST['signoff_status'] . "', rcf_date2 = '$newdate' WHERE id_incr = ".$_POST['id_incr'].""; if (isset($sql) && !empty($sql)) { //echo "<!--" . $sql . "-->"; $result = mysql_query($sql) or die ("Invalid query: " . mysql_error()); } header("Location: displayactive.php"); ?> If you are redirecting you do not need to echo anything. Echoing anything before a header command will cause it to not work. You will also notice I removed the "done" why are you outputting done if you are just redirecting? Anyhow, I hope this helps you better to understand what is going on. Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771233 Share on other sites More sharing options...
jeff5656 Posted February 26, 2009 Author Share Posted February 26, 2009 Thanks! that finally explains it to me. I agree about the javascript half-ass thing. What's the point of all the javascript just because you want to re-direct after some php code? Quote Link to comment https://forums.phpfreaks.com/topic/146761-display-header-problem/#findComment-771495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.