1 Replies - 1168 Views - Last Post: 21 May 2008 - 11:59 AM Rate Topic: -----

#1 mdgottfried  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-May 08

apgform handler error

Post icon  Posted 20 May 2008 - 02:33 PM

I am a teacher and Iíve only started using php to handle my studentís form submissions. Iíve been using a tripod web page, where they had the form handler software as a library, and I didnít have to know what I was doing. Their advertisements are becoming a problem, so, Iíve set up my own page and I was delighted to find a form handler that saved the data to an excel file.

But, although I changed nothing below the line, I get the following message when I run this free formhandler:

Notice: Undefined variable: header in /www/rack111.com/m/d/g/mdgottfried/htdocs/apg/apgformtest.php on line 53

Notice: Undefined variable: data in /www/rack111.com/m/d/g/mdgottfried/htdocs/apg/apgformtest.php on line 66

Warning: Cannot modify header information - headers already sent by (output started at /www/rack111.com/m/d/g/mdgottfried/htdocs/apg/apgformtest.php:53) in /www/rack111.com/m/d/g/mdgottfried/htdocs/apg/apgformtest.php on line 85

I canít tell what is wrong on lines 53, 66 or 85Ö. And would really appreciate any help you might be able to give me. (I get the same error with 3 different test forms.)

Obviously the code isn't mine....I'm having trouble implementing this php formhandler. )

The formhandler is free, I'm not stealing anything or cheating. I'm just too busy teaching biology to teach myself enough code to do more than modify a web page here or there.

Any help would be appreciated.
Mark

<?php 

/*		 

			apgForm 1.7

			By: Alvaro Prieto (apg88)

			E-Mail: webmaster@apg88.com

			Web site: http://www.apg88.com/index.php?page=apgForm

			

			

			Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/legalcode

			http://creativecommons.org/licenses/by/2.5/ (Summary)

			You can modify the code all you want, make money with it, release newer versions, etc...  just leave my name, email, and web site URL on the it.

			

*/

						

			// Change this to the page you want the user to be redirected to after form is submitted (i.e. success.html and error.html)

			$success = "";

			$error = "";

			

			// Change this to the character(s) you want to be placed instead of line breaks(new line, enter, etc)

			$lbChar = " ";   // default is a space, you may change it to whatever you want

			

			// Don't change anything below this line			  

			

			// Determine if the form was sent through the GET methog or the POST method.

			if($_POST){

						$array = $_POST;

			} else if($_GET){								 

						$array = $_GET;

			} else {

									die("You must Access this file through a form.");			// If someone accesses the file directly, it wont work :)

			}		  

 

			//Check if the filename was sent through the form or not

			if(!$array['filename']){

						// if the filename wasnt sent through the form, it will become form.xls, you can change the default if you want.

						$array['filename'] = "form.xls";  //Set the file to save the information in

			

			} else {

						if(!(stristr($array['filename'],".xls"))){

									$array['filename'] = $array['filename'] . ".xls";

						}

			}

			

			// Define the tab and carriage return characters:

			$tab = "\t";		//chr(9);

			$cr = "\n";					 //chr(13);

			

			if($array){

									// Make The Top row for the excel file and store it in the $header variable

									$keys = array_keys($array);

									foreach($keys as $key){

												if(strtolower($key) != 'filename' && strtolower($key) != 'title'){ 

															$header .= $key . $tab;

												}

									}

									$header .= $cr;

									

									//Make the line with the contents to write to the excel file.

									foreach($keys as $key){

												if(strtolower($key) != 'filename' && strtolower($key) != 'title'){ 

 

															$array[$key] = str_replace("\n",$lbChar,$array[$key]);

															$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);

															$array[$key] = str_replace("\\","",$array[$key]);

															$array[$key] = str_replace($tab, "	", $array[$key]);

															$data .= $array[$key] . $tab;

												}

									}

									$data .= $cr;

									

									if (file_exists($array['filename'])) {

												$final_data = $data;				  // If the file does exist, then only write the information the user sent

									} else {

												$final_data = $header . $data;			   // If file does not exist, write the header(first line in excel with titles) to the file

									}

									// open the file and write to it

									

									$fp = fopen($array['filename'],"a"); // $fp is now the file pointer to file $array['filename']

									

									if($fp){

												

												fwrite($fp,$final_data);  //Write information to the file

												fclose($fp);				   // Close the file

												// Success

												header("Location: $success");

									} else {

												// Error

												header("Location: $error");

									}

			}

			

?>

 


Is This A Good Question/Topic? 0
  • +

Replies To: apgform handler error

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: apgform handler error

Posted 21 May 2008 - 11:59 AM

Well the first two errors about an undefined variable means that you have attempted to use a variable which hasn't been explicitly set (defined). Now you will see that this is defined as a "notice" not an error. While it isn't exactly an error, if the strict level of error reporting is enabled then it will notify you of all violations in "proper" programming standards. In programming it is "proper" to define all variables before you attempt to use them. PHP will still work with variables not previous defined, but the error reporting will notify of this fact. It is considered good to have the strict reporting on so that it points out things like this because variables that are not defined could potentially be places where bugs may occur. If it is not defined, it is unknown.

Now as far as your warning there, it is saying that before you used your header() function call (which adds headers to the request for the server) you have already outputted some content to the request. once you add content to the request headers are automatically sent and thus you can't attempt to modify them later. This is a rather common error and might be related to the first two notices since this error would have appeared on the old host as well prior to your move. So fix the first two errors and see if that helps with the third. If not, then find where in the script you might be sending content to the page triggering the headers to be sent prior to you using header().

You can read up a little more on this error at the PHP manual page listed below...

PHP: header - manual

Good luck and hopefully this will help you out. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1