Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 135,943 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,741 people online right now. Registration is fast and FREE... Join Now!




apgform handler error

 
Reply to this topicStart new topic

apgform handler error

mdgottfried
20 May, 2008 - 01:33 PM
Post #1

New D.I.C Head
*

Joined: 20 May, 2008
Posts: 2

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

CODE

<?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");

                                    }

            }

            

?>




User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Apgform Handler Error
21 May, 2008 - 10:59 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:42AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month