I am using this script I found (apgForm1.7)to process a form and save into a spreadsheet. It saves the data fine, however it will not redirect the user and it gives me errors that I can't figure out. (yeah... newie, sorry)
I've looked all up the functions and common problems associated and from what I can tell everything seems to be the way it should be. Could the problem be with a php setting?
Any advice would be greatly appreciated!!!
Errors:
---------
Notice: Undefined variable: header in acss/forms/feedback.php on line 89 Notice: Undefined variable: data in acss/forms/feedback.php on line 115 Warning: Cannot modify header information - headers already sent by (output started at acss/forms/feedback.php:89) in acss/forms/feedback.php on line 152
lines referenced in error:
---------------------------
89: $header .= $key . $tab;
115: $data .= $array[$key] . $tab ;
152: header("Location: $success");
The Script:
-----------------
<?php
//redirected to after form is submitted
$success = "success.html";
$error = "error.htm";
//character(s)to be placed instead of line breaks(new line, enter, etc)
$lbChar = " "; // default space
// 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.");
}
//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");
}
}
?>