Kar606 Posted April 8, 2009 Share Posted April 8, 2009 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"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/ Share on other sites More sharing options...
jackpf Posted April 8, 2009 Share Posted April 8, 2009 That error is because you're sending something to the page before you're sending the header. You can either find where something's being sent to the page and remove it, or put ob_start(); at the top of your script and ob_end_flush(); at the bottom...that should fix it. And in response to your notification (not really a real error) you can just ignore it. If you really want to get rid of it however, just put $header = ''; somewhere before you add onto it with $header .= Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-804717 Share on other sites More sharing options...
Kar606 Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks for you reply.... I tried the ob_start(); and ob_end_flush(); at the beggining and end of the script and it got rid of the errors, however it still does not redirect the page after the user submits the form. Also, I thought the previous way was correct because header("Location: ") was declared in a variable before the information was sent. -- I could totally be wrong about that.... (I'm really new) I would still like to know how to get the form to redirect the user to the success.html page. any other suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-804836 Share on other sites More sharing options...
Kar606 Posted April 8, 2009 Author Share Posted April 8, 2009 Well, I got it!! I needed to put exit (); after header("Location: $success") And make sure $success = "success.htm"; was at the very very very top. Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-804983 Share on other sites More sharing options...
jackpf Posted April 9, 2009 Share Posted April 9, 2009 Them two factors reeeally should make no difference whatsoever. As long as you declare $success before you send the header, it shouln't matter, and as for the exit(), all that does is tell the script to stop processing, which it does at the end anyway. Ahh well, if it works it works Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-805351 Share on other sites More sharing options...
Kar606 Posted April 9, 2009 Author Share Posted April 9, 2009 Ok so I was a little too optimistic.... exit(); does help by ending the script there for forcing it to go to redirect success.html (it won't work without it) but for some reason it seems to run the script twice... or at least it adds two rows of the same data to the spreadsheet document. ??? I really have no clue what is going on, why would that effect it? Is there any more information I can provide that might help solve this? Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-805572 Share on other sites More sharing options...
jackpf Posted April 9, 2009 Share Posted April 9, 2009 Hmm...I just tried your original script and it seemed to work. Btw, it doesn't seem like a very safe script - the way it loops through each superglobal is a well known method hackers use to include their own files in other people's scripts. Idk, it may be fine, but it's not the way I would do it... Quote Link to comment https://forums.phpfreaks.com/topic/153191-apgform17-form-redirect-problem/#findComment-805712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.