Jump to content

Any replacement for header(location) ? in this code it split error instead of redirecting when i put the php after writing comment section, but when i put it before it, it does work, even just the comments section when i put it in different file all alone it does work normally. Would take any suggestion.

 

WHOLE CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<link href="https://fonts.googleapis.com/css?family=Alegreya+Sans" rel="stylesheet">
<link rel="stylesheet" href="style.css" type="text/css">
<!-- #BeginTemplate "master.dwt" -->

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<!-- #BeginEditable "doctitle" -->
<title>VVT37.CZ</title>
<style type="text/css">


.style2 {
	font-size: large;
	text-align: left;
	background-color: #FFFFFF;
	border-bottom-style: none;
	border-bottom-color: #000000;
}
.style15 {
	text-align: center;
	background-color: #F8E8D9;
}
.style18 {
	text-align: center;
	border-bottom-style: solid;
	border-bottom-width: 1px;
	border-bottom-color: #333333;
}
.style20 {
	font-size: 4px;
	text-align: center;
	color: #723007;
	border-bottom-style: none;
	border-bottom-color: #000000;
	background-color: #FFFFFF;
}
.style21 {
	text-align: center;
	font-size: large;
	color: #4F3E2B;
	border-bottom-style: none;
	border-bottom-width: 1px;
	border-bottom-color: #333333;
}
.style24 {
	font-size: small;
}
.style25 {
	color: #000000;
	text-align: center;
	background-color: #FCCD9C;
}
.style26 {
	border-bottom: 2px solid #FABA6F;
	background-color: #FFFAF4;
	color: #000000;
	background-image: none;
	text-align: center;
}
.style28 {
	border-bottom: 1px none #333333;
	text-align: center;
	font-size: 12pt;
	color: #4F3E2B;
	background-color: #FFE1C1;
	}
.style29 {
	text-align: center;
	background-color: #F8BF86;
}
.style30 {
	border-bottom: 1px solid #333333;
	text-align: left;
	background-color: #FFF5EA;
}
.style32 {
	border-bottom: 1px solid #333333;
	text-align: center;
	color: #D50404;
	background-color: #F8E8D9;
}
.style34 {
	color: #01016D;
}
.style35 {
	color: #000000;
}
.style36 {
	color: #F58D23;
}
.style37 {
	font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande", "Lucida Sans", Arial, sans-serif;
	font-size: small;
}
.style38 {
	text-decoration: none;
}
.style39 {
	border-bottom: 1px solid #333333;
	text-align: left;
	background-color: #FFDCB9;
}
.style40 {
	background-color: #F8E8D9;
}
.style41 {
	background-color: #FFFAF4;
}
</style>

<!-- #EndEditable -->
<link href="styles/style1.css" media="screen" rel="stylesheet" title="CSS" type="text/css" />
<style type="text/css">


.style1 {
	font-size: xx-small;
}
.style2 {
	background-color: #FFFFFF;
}
.style3 {
	border-width: 0;
}
.style4 {
	font-size: small;
}
</style>

</head>

<body>
	

<!-- Begin Container -->
<div id="container">
	<!-- Begin Masthead -->
	<div id="masthead">
		<table cellpadding="0" cellspacing="0" style="width: 900px">
			<tr>
				<td style="width: 299px; height: 107px; text-align: left;">
				&nbsp;</td>
				<td style="width: 300px; height: 107px;">&nbsp;</td>
				<td style="width: 300px; height: 107px; text-align: right;">
				&nbsp;</td>
			</tr>
			</table>
	</div>
	
	<!-- End Masthead -->
	<!-- Begin Navigation -->
	<div id="navigation">
		
		<ul>
			<li><a href="index.html">Úvod</a></li>
			<li><a href="stranky/partneri.htm">Partneři turnaje</a></li>
			<li><a href="stranky/realizacni%20tym.htm">Realizační tým</a></li>
			<li><a href="stranky/mapa.htm">Mapka</a></li>
			<li><a href="stranky/fotogalerie.htm">Fotogalerie</a></li>
			<li><a href="stranky/dokumenty.html">Dokumenty</a></li>
			<li><a href="seznamka.php">Seznamka</a></li>
		</ul>
					</td>
				</tr>
				<tr>
					<td class="style2">
					<img alt="" height="130" src="images/A-vetkom-veter.nav.gif" width="140" style="padding-top:20px;" /></td>
				</tr>
				</table>
			</li>
		</ul>
	</div>
	
	<!-- End Navigation -->
	<!-- Begin Content -->
	<div id="content">
		
		<!-- #BeginEditable "content" -->
		<table cellpadding="0" cellspacing="0" style="width: 100%">
		<form action="" method="POST">

<label> Vaše Jméno 
 <input type="text" name="Name" class="Input" style="width: 225px" required>
</label>
<label> e-mail:
 <input type="text" name="Mail" class="Input" style="width: 225px" required>
</label>
<label> Předmět:
 <input type="text" name="Predmet" class="Input" style="width: 225px" required>
</label>
<br><br>
<label> Vaše Zpráva: <br>
 <textarea name="Comment" class="Input" style="width: 300px" required></textarea>
</label>
<br><br>
<input type="submit" name="Submit" value="Submit Comment" class="Submit">

</form>
<?php
 
 if(isset($_POST['Submit'])){
  header("Location: Seznamka.php");
  print "<h1>Your comment has been submitted!</h1>";

  $Name = $_POST['Name'];
  $Mail = $_POST['Mail'];
  $Predmet= $_POST['Predmet'];
  $Comment = $_POST['Comment'];

  #Get old comments
  $old = fopen("comments.txt", "r+t");
  $old_comments = fread($old, 1024);

  #Delete everything, write down new and old comments
  $write = fopen("comments.txt", "w+");
  $string =    
                 "<div class='ahoj'><i>Autor:</i> <b>".$Name."</b>".$Mail."<br> <i> Předmět:</i> ".$Predmet."<br>".$Comment."</br></div>\n".$old_comments;
  fwrite($write,     $string,);
  fclose($write);
  fclose($old);
 }

 #Read comments
 $read = fopen("comments.txt", "r+t");
 echo "<br><br>Comments<hr>".fread($read, 1024);
 fclose($read);

?>
		</table>

		

		<!-- #EndEditable "content" --></div>
		</html>
	<!-- End Content -->
	<!-- Begin Footer -->
	<div id="footer">
		<table style="width: 100%">

				Návrh a správa webu: 

			</tr>
		</table>
	</div>
	<!-- End Footer --></div>
<!-- End Container -->

</body>

<!-- #EndTemplate -->
</html>

JUST COMMENT SECTION HTML PHP

<html>
<link href="https://fonts.googleapis.com/css?family=Alegreya+Sans" rel="stylesheet">
<head>
 <link rel="stylesheet" href="style.css" type="text/css">
</head>

<body>



 <?php
 
 if(isset($_POST['Submit'])){
  header("Location: Seznamka.php");
  print "<h1>Your comment has been submitted!</h1>";

  $Name = $_POST['Name'];
  $Mail = $_POST['Mail'];
  $Predmet= $_POST['Predmet'];
  $Comment = $_POST['Comment'];

  #Get old comments
  $old = fopen("comments.txt", "r+t");
  $old_comments = fread($old, 1024);

  #Delete everything, write down new and old comments
  $write = fopen("comments.txt", "w+");
  $string =    
                 "<div class='ahoj'><i>Autor:</i> <b>".$Name."</b>".$Mail."<br> <i> Předmět:</i> ".$Predmet."<br>".$Comment."</br></div>\n".$old_comments;
  fwrite($write,     $string,);
  fclose($write);
  fclose($old);
 }

 #Read comments
 $read = fopen("comments.txt", "r+t");
 echo "<br><br>Comments<hr>".fread($read, 1024);
 fclose($read);

?>
</body>

</html>

 

Link to comment
https://linustechtips.com/topic/1041300-replacement-for-headerlocation/
Share on other sites

Link to post
Share on other sites

The header function cannot be used after any data is sent, because the header is already sent before the data is sent.

 

You can use this HTML tag to redirect to the page:

<meta http-equiv="refresh" content="0; URL='Seznamka.php'" />

(the 0 is the number of seconds before the redirect takes place.)

🙂

Link to post
Share on other sites

You could also move the php code at the beginning, before you output anything to the user.

You can also use ob_start()  to buffer everything so you make sure nothing is sent until either script finishes, or you say ob_end_flush

 

Here's the code changed so that you don't even need to redirect user.

 

<?php

$submitted = FALSE;

if(isset($_POST['Submit'])){
	$Name = $_POST['Name'];
	$Mail = $_POST['Mail'];
	$Predmet= $_POST['Predmet'];
	$Comment = $_POST['Comment'];
	
	$old_comments = file_get_contents('comments.txt'); // reads the whole file, not just first 1024 bytes.
	
	$string = '<div class="ahoj">'.
			  '<i>Autor:</i> <b>'.htmlspecialchars($Name).'</b> '.htmlspecialchars($Mail).'<br> '.
			  '<i> Předmět:</i> '.htmlspecialchars($Predmet)."<br>".htmlspecialchars($Comment).'</br>'.
			  '</div>\n';
	$result = file_put_contents('comments.txt',$string.$old_comments);
	$submitted = TRUE;
 }
<html>
<link href="https://fonts.googleapis.com/css?family=Alegreya+Sans" rel="stylesheet">
<head>
 <link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>

 <?php
 
 if($submitted==TRUE){
  print "<h1>Your comment has been submitted!</h1>";
 }

 #Read comments
 $read = file_get_contents("comments.txt");
 echo "<br><br>Comments<hr>".$read;


?>
</body>
</html>

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×