Ok, I am walking my way through an instructional php book and basically going over the basics again. After I finished doing the script the way the book said to do it, I kept getting a header error saying that the headers have already been submitted. The book had my echo statements spread out all over the place instead of just doing a self processing file. You know? The kind that looks back at itself for print commands once a submit button his clicked. SO I modified the file to be self contained. Seems to work with just one problem that I cannot figure out how to fix. It keeps adding the same submission to my database every time the refresh button is clicked. So until you add a new joke to it, the old one will be re-added. And the mayhem continues with the new joke. (It's a lousy joke database for testing purposes) Here's the code:

<?php

	if (get_magic_quotes_gpc())
	{
		function stripslashes_deep($value)
		{
			$value = is_array($value) ?
			array_map('stripslashes_deep', $value) :
			stripslashes($value);
			return $value;
		}
		$_POST = array_map('stripslashes_deep', $_POST);
		$_GET = array_map('stripslashes_deep', $_GET);
		$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
		$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
	}
	
		if (isset($_GET['addjoke']))
	{
		echo '<form action="?" method="post">
		<div>
		<label for="joketext">Type your joke here:</label>
		<textarea id="joketext" name="joketext" rows="3" cols="40">
		➥</textarea>
		</div>
		<div><input type="submit" value="Add"/></div>
		</form>';
		exit();
	}
	else
	{
		echo '<p><a href="?addjoke" >Add your own joke<a></p>';
	}
	
		echo '<p>Here are all the jokes in the database:</p>';


	$link = mysqli_connect('********************', '****', '*********************');
	if (!$link)
	{
		$output = 'Unable to connect to the database server' .mysqli_error($link). ' ';
		include 'output.html.php';
		exit();
	}
	
	if (!mysqli_set_charset($link, 'UTF8'))
	{
		$output = 'unable to set database connection encoding ' .mysqli_error(). ' ';
		include 'output.html.php';
		exit();
	}
	
	if (mysqli_select_db($link, 'dante2'))
	{
		echo 'Database connection established.';
	}
	else
	{
		echo 'Unable to locate the dante2 database. ' .mysqli_error($link). ' ';
	}
	
	
		if (isset($_POST['joketext']))
	{
		$joketext = mysqli_real_escape_string($link, $_POST['joketext']);
		
		$sql = 'INSERT INTO joke SET
			joketext = " ' .$joketext. ' ", 
			jokedate = CURDATE() ';
			
		if (!mysqli_query($link, $sql))
		{
			echo 'Error adding submitted joke: ' .mysqli_error($link);
		}
		
		foreach ($jokes as $joke) 
		{
			echo "<blockquote><p>$joke</p></blockquote>";
		}
		
	}
	
	$result = mysqli_query($link, 'SELECT joketext FROM joke');
	if (!$result)
	{
		echo 'Error fetching jokes: ' .mysqli_error($link);
		exit();
	}
	
	
	
	while ($row = mysqli_fetch_array($result))
	{
		$jokes[] = $row['joketext'];
	}
	
	foreach ($jokes as $joke) 
		{
			echo "<blockquote><p>$joke</p></blockquote>";
		}
		
?>

I know I am missing something, I'm just not sure what. I believe the submission is being held in memory somewhere, I'm just aware of how to clear it. Thanks for any pointers. =)Heck, I'll settle for hints. I'm not lazy, just lost atm.

Member Avatar for diafol

something tell me you have a header() command in

output.html.php

If so, you can't do it like this. header must be placed before any output (html, echo, print, error msg, html whitespace, var_dump, print_r ...).

*facepalm* Maybe I missed a character or something last time, because I tried stripping all of the html from the output file a while back and got the same error. Now it's working. No, actually I left the html at the end of the file. That might have been the monkey wrench. Well, thank you. The script is working and not resubmitting every time I hit refresh. You rock ardav. =)

One more question, though. After you hit the submit button and you get the joke added to the table, you are supposed to be presented with a link and a request to add another joke, that's not happening. Is there something I need to clear from a session or an unseen cookie?

Member Avatar for diafol
if (isset($_GET['addjoke']))
	{
		echo '<form action="?" method="post">
		<div>
		<label for="joketext">Type your joke here:</label>
		<textarea id="joketext" name="joketext" rows="3" cols="40">
		</textarea>
		</div>
		<div><input type="submit" value="Add"/></div>
		</form>';
		exit();
	}

The thing is the form sends to ? as opposed to '?addjoke=1' or similar. If you change the action to this, it should satisfy the

if (isset($_GET['addjoke']))

conditional

Oddly enough, when I stripped all the html out of the output.html.php file, it would display all of the jokes just fine. And the link to give you the option to post a new joke. The problem then is that after you hit submit on the form, you get that same header error message. Even add the extra snippet that you recommend. I'm going to just upload the files here for you to look at. I am at a complete loss. I mean, the header("Location: .") call directs us back to the index.php file so I am not understanding how the headers on the form could effect the headers on the index.php. =(

Wonder of wonders and miracles of miracle! I've been hacking at this script for almost 3 days now non stop. I'm still convinced I did something wrong somewhere, but I managed to get the whole thing to work as it should. When I was modifying the control script to be independent from the error.html.php, form.html.php and other scripts, I had to place all of their print statements into the index.php file so the information from the script could be seen on the monitor.(duh) \

After I did this I was still getting the same errors and it puzzled me, cause I wasn't relying on external files with html headings and such. Then it hit me, At the top of the control structure I left the IF statement for checking to see if someone wanted to add a joke. it turns out that it was the html print out from this If statement in conjunction with the echo from the database select call that was messing everything up. Once I moved the If statement below the header call and removed the connection confirmation for the database (it'll be self evident if it can't connect) Everything worked just fine.

I tried moving these statements around in the original control script and I met with equally pleasing results. I'm going to move on with the rest of the book, but I would still like to know what it is that I did wrong in the first place. Obviously these scripts were tested before they were printed, so I'm sure I goofed somewhere. Thank you for all of your help and suggestions. =) Very much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.