April 2, 200917 yr Morning Everyone, I am having a little trouble with PHP and Header Location. I know this has been covered in other topics, but I don't really understand what the problem is and how it is fixed. Here is my code: <form method="post" action=""> <?php include("config.php"); if (isset($_POST["submit"])) { $txt = $_POST['txt']; $txt = mysql_real_escape_string($_POST['txt']); $result = mysql_query("UPDATE content SET txt='$txt' WHERE id='1'", $db); } if ($result) { header("Location: " . $_SERVER['php_SELF']); } else { $result = mysql_query("SELECT * from content WHERE id='1'", $db); while ($row=mysql_fetch_assoc($result)){ require('fckeditor/fckeditor.php'); $oFCKeditor = new FCKeditor('txt'); $oFCKeditor->BasePath = 'fckeditor/'; $oFCKeditor->Value = $row['txt']; $oFCKeditor->Height = '300'; $oFCKeditor->Create(); } } ?> <br /><br /> <input type="submit" name="submit" value="Update Page"/> </form> And here are my errors: Notice: Undefined index: php_SELF in /Applications/MAMP/htdocs/website.com/admin/index.php on line 72 Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/website.com/admin/index.php:6) in /Applications/MAMP/htdocs/website.com/admin/index.php on line 72 As you can see I am working in MAMP, is it just because it is in MAMP or would I get the same problem on an online server? Thanks in advance for anyones assistance.
April 2, 200917 yr I have had problems with the same error in the past and it was related to there being space before my opening php tags. I am not familiar however with a php generated form, as mine was html then processed by php instead.... I would say however that the problem is not related to MAMP, rather it is just telling you what the error is that is in your code. I remember spending ages looking for a resolution to the header issue and found a useful doc explaining possible reasons; will look now and post when find.. Argh I'm still trying to find it.... BTW is that your complete code? OK found a bit: The headers already sent error means that the web browser was sent something - html, plain text, even a space - prior to using the header() function(Source: PHP6 and MySql 5 - Larry Ullman) Will keep looking ... Ah ... White space exists in the script before php tags, data has already been printed, or a file has been included. Sorry, don't know if i have helped or not. Please keep me posted as would like to know what the prob is
April 2, 200917 yr Author Thanks for the reply, No here is the complete code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Alderney Commission for Renewable Energy</title> <style type="text/css"> @import url("reset.css"); </style> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <?php include("inc_top.php"); ?> <div id="hero" class="home_hero"></div> <div id="hero_bottom" class="with_sub"></div> <div id="content"> <div id="left_column"> <div id="left_nav"> <ul> <li><a href="link.php">TIDE AND WAVE RESOURCE </a> </li> <li class="no_border"><a href="link.php">BLOCK MAPS </a> </li> </ul> </div> <div id="news"> <h6>LATEST NEWS</h6> <div id="news_holder"> <div class="news_item"> <span class="date">1st March 2009</span> <p>New Alderney Commission for Renewable Energy website goes live. <a href="link.php">read more</a></p> </div> <div class="news_item"> <span class="date">22nd July 2008</span> <p>The Renewable Energy (Aldeney) Law, 2007 receives Royal Assent. <a href="link.php">read more</a></p> </div> <div class="news_item"> <span class="date">June 2008</span> <p>Application for Consent received from Alderney Renewable Energy Limited (ARE) and OpenHydro Limited for the deployment of up to four turbines in the Alderney Race, together with subsea and onshore cabling. <a href="link.php">read more</a></p> </div> <a href="link.php" class="cta_view_all">View all news items.</a> </div> </div> </div> <div id="main_column"> <h1 class="header_home">About Alderney</h1> <p> <form method="post" action=""> <?php include("config.php"); if (isset($_POST["submit"])) { $txt = $_POST['txt']; $txt = mysql_real_escape_string($_POST['txt']); $result = mysql_query("UPDATE content SET txt='$txt' WHERE id='1'", $db); } if ($result) { header("Location: " . $_SERVER['php_SELF']); } else { $result = mysql_query("SELECT * from content WHERE id='1'", $db); while ($row=mysql_fetch_assoc($result)){ require('fckeditor/fckeditor.php'); $oFCKeditor = new FCKeditor('txt'); $oFCKeditor->BasePath = 'fckeditor/'; $oFCKeditor->Value = $row['txt']; $oFCKeditor->Height = '300'; $oFCKeditor->Create(); } } ?> <br /><br /> <input type="submit" name="submit" value="Update Page"/> </form> </p> </div> <div class="clearing">Content for class "clearing" Goes Here</div> <?php include("inc_footer.php"); ?> </div> </div> </body> </html>
April 2, 200917 yr Hi guys, the problem is that headers can only be called before anything - absolutely anything is sent to the browser. That anything can be whitespace before your opening <?php tag, html or javascript. Basically nothing can be sent before them. You can process anything you like before it as long as it doesntget sent to the browser. So, DFT; Yours is a simple fix in that you can move the <form> tag and line below the <?php ?> area and it will be fine, just make sure that the <?php is on line 1, column 1 of your file. HTH Spike
April 2, 200917 yr Author Thanks Spike, I do understand now. if ($result) { header("Location: " . $_SERVER['php_SELF']); } Am I actually doing this right? Would there be a better way of doing this? Once the user has updated the text in the text editor, and hits 'Update Page' all I want is for the page to refresh. I used to use this: echo "<meta http-equiv=Refresh content=0;url=index.php>"; What is best practice? Thanks
April 2, 200917 yr yes the usage is correct but the syntax is: if ($result) { header("Location: " . $_SERVER['PHP_SELF']); exit(); } PHP_SELF is all uppercase and stcik an exit() in to stop any further processing below the header.
Create an account or sign in to comment