adamriley Posted January 9, 2010 Share Posted January 9, 2010 hi ---------------------------------------------------------------------------------------------------- error log "[sat Jan 09 17:40:00 2010] [error] [client 127.0.0.1] PHP Notice: Undefined variable: a1 in C:\\web\\htdocs\\Filey\\2.php on line 2, referer: http://localhost/Filey/1.php" "[sat Jan 09 17:40:00 2010] [error] [client 127.0.0.1] PHP Notice: Undefined variable: a3 in C:\\web\\htdocs\\Filey\\2.php on line 3, referer: http://localhost/Filey/1.php" "[sat Jan 09 17:40:00 2010] [error] [client 127.0.0.1] PHP Notice: Undefined variable: a2 in C:\\web\\htdocs\\Filey\\2.php on line 4, referer: http://localhost/Filey/1.php" --------------------------------------------------------------------------------------------------- 2.php ---------------------------------------------------------------------------------------------------- php $filler1 = "<html><head><title>$a1</title>"; $filler2 = "<body>$a3"; $filler3 = "<div>$a2</div></body></html>"; $a1 = $_POST["PN"]; $a2 = $_POST["Date_name"]; $a3 = $_POST["Note"]; $myFile = "notenw.html"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "$filler1"; $stringData1 = "$filler2"; $stringData2 = "$filler3"; fwrite($fh, "$stringData\n"); fwrite($fh, "$stringData1\n"); fwrite($fh, "$stringData2\n"); fclose($fh); ?> ------------------------------------------------------------------------------------------------- 1.php <?php require_once $_SERVER['DOCUMENT_ROOT'] . '/Login/common.php';?> <?php if ($_SESSION['userName'] == 'Adamriley') { $name = 'Adam lee riley';} else { $name = 'Nathan Warburton';} ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Untitled Page</title> <script language="JavaScript" type="text/javascript"> <!-- function ValidateForm1(theForm) { var strFilter = /^[A-Za-zƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ \t\r\n\f0-9-]*$/; var chkVal = theForm.TextArea1.value; if (!strFilter.test(chkVal)) { alert("In your note you may only have a maximum of 100 letters"); theForm.TextArea1.focus(); return false; } if (theForm.TextArea1.value == "") { alert("In your note you may only have a maximum of 100 letters"); theForm.TextArea1.focus(); return false; } if (theForm.TextArea1.value.length < 2) { alert("In your note you may only have a maximum of 100 letters"); theForm.TextArea1.focus(); return false; } if (theForm.TextArea1.value.length > 100) { alert("In your note you may only have a maximum of 100 letters"); theForm.TextArea1.focus(); return false; } return true; } //--> </script> </head> <body bgcolor="#FFFFFF" text="#000000"> <div id="bv_Form1" style="position:absolute;left:58px;top:59px;width:704px;height:356px;z-index:5;" align="left"> <form name="Form1" method="POST" action="2.php" id="Form1" onsubmit="return ValidateForm1(this)"> <input type="text" id="Editbox2" style="position:absolute;left:94px;top:36px;width:483px;font-family:Courier New;font-size:16px;z-index:0" name="Date_name" value="<?php echo date("F j, Y, g:i a"); ?> || <?php echo $name ?>" readonly="readonly"> <input type="text" id="Editbox1" style="position:absolute;left:93px;top:4px;width:489px;font-family:Courier New;font-size:16px;z-index:1" name="PN" value="Page name (title)"> <textarea name="Note" id="TextArea1" style="position:absolute;left:81px;top:63px;width:547px;height:215px;font-family:Courier New;font-size:16px;z-index:2" rows="10" cols="51"></textarea> <input type="submit" id="Button1" name="Button1" value="Save changes" style="position:absolute;left:336px;top:293px;width:194px;height:41px;border:3px #98FB98 solid;background-color:#0000FF;color:#FFFF00;font-family:Bookman Old Style;font-size:27px;z-index:3"> <input type="reset" id="Button2" name="Reset" value="Reset note" style="position:absolute;left:158px;top:291px;width:167px;height:44px;border:3px #FFFF00 dotted;background-color:#00FF00;color:#FF0000;font-family:Arial;font-size:27px;z-index:4"> </form> </div> <div id="bv_Text1" style="position:absolute;left:334px;top:9px;width:150px;height:32px;z-index:6;" align="left"> <font style="font-size:27px" color="#FF0000" face="Arial"><b>New note</b></font></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/ Share on other sites More sharing options...
wildteen88 Posted January 9, 2010 Share Posted January 9, 2010 Variables must be defined before you can use them. $filler1 = "<html><head><title>$a1</title>"; $filler2 = "<body>$a3"; $filler3 = "<div>$a2</div></body></html>"; $a1 = $_POST["PN"]; $a2 = $_POST["Date_name"]; $a3 = $_POST["Note"]; The last three lines need to be before the first three. Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/#findComment-991777 Share on other sites More sharing options...
adamriley Posted January 9, 2010 Author Share Posted January 9, 2010 Thanks again wildteen88 Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/#findComment-991780 Share on other sites More sharing options...
adamriley Posted January 9, 2010 Author Share Posted January 9, 2010 Hi this is an new error but the error is the same! ----------------------------------------------------------------------------------------------------- error log "[sat Jan 09 18:49:32 2010] [error] [client 127.0.0.1] PHP Parse error: syntax error, unexpected T_VARIABLE in C:\\web\\htdocs\\Filey\\2.php on line 12, referer: http://localhost/Filey/1.php" ----------------------------------------------------------------------------------------------------------- 2.php ---------------------------------------------------------------------------------------------------- <?php $a1 = $_POST["PN"]; $a2 = $_POST["Date_name"]; $a3 = $_POST["Note"]; $filler1 = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"; $filler2 = "<html"; $filler2 = "<head>"; $filler3 = "<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>"; $filler4 = "<title>$a1</title>"; $filler5 = "</head>"; $filler6 = "<body bgcolor="#FFFFFF" text="#000000">"; $filler7 = "<div id="bv_Text1" style="position:absolute;left:34px;top:34px;width:713px;height:32px;z-index:0;" align="left">"; $filler8 = "<font style="font-size:27px" color="#000000" face="Arial"><b>Note name:$a1</b></font></div>"; $filler9 = "<div id="bv_Text2" style="position:absolute;left:12px;top:76px;width:781px;height:32px;z-index:1;" align="left">"; $filler10 = "<font style="font-size:27px" color="#000000" face="Arial"><b>Date and time:$a2</b></font></div>"; $filler11 = "<textarea name="TextArea1" id="TextArea1" style="position:absolute;left:41px;top:134px;width:743px;height:391px;font-family:Courier New;font-size:16px;z-index:2" rows="20" cols="70" readonly="readonly">$a3</textarea>"; $filler12 = "</body>"; $filler13 = "</html>"; $myFile = "notenw.html"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "$filler1"; $stringData1 = "$filler2"; $stringData2 = "$filler3"; $stringData3 = "$filler4"; $stringData4 = "$filler5"; $stringData5 = "$filler6"; $stringData6 = "$filler7"; $stringData7 = "$filler8"; $stringData8 = "$filler9"; $stringData9 = "$filler10"; $stringData10 = "$filler11"; $stringData11 = "$filler12"; $stringData12 = "$filler13"; fwrite($fh, "\n"); fwrite($fh, "$stringData1\n"); fwrite($fh, "$stringData2\n"); fwrite($fh, "$stringData3\n"); fwrite($fh, "$stringData4\n"); fwrite($fh, "$stringData5\n"); fwrite($fh, "$stringData6\n"); fwrite($fh, "$stringData7\n"); fwrite($fh, "$stringData8\n"); fwrite($fh, "$stringData9\n"); fwrite($fh, "$stringData10\n"); fwrite($fh, "$stringData11\n"); fwrite($fh, "$stringData12\n"); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/#findComment-991813 Share on other sites More sharing options...
teamatomic Posted January 9, 2010 Share Posted January 9, 2010 Double quotes within double quotes must be escaped. $filler6 = "<body bgcolor=\"#FFFFFF\" text=\"#000000\">"; If you do not need to extrapolate variables and an echo line contains double quotes as your does then enclose the line in single quotes. $filler6 = '<body bgcolor="#FFFFFF" text="#000000">';// is good $filler6 = '<body bgcolor="#FFFFFF" text="$color">';// is bad,it will echo exactly as you see it, color wont extrapolate...but $filler6 = "<body bgcolor=\"$bg_color\" text=\"$text_color\">";// is good HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/#findComment-991822 Share on other sites More sharing options...
wildteen88 Posted January 9, 2010 Share Posted January 9, 2010 You don't need to define a variable for every line. Variable assignment can span multiple lines, example $myVar = "Line1 Line2 Line3 ... LineXYZ etc"; There is no need to reassign your variables here $stringData = "$filler1"; $stringData1 = "$filler2"; $stringData2 = "$filler3"; $stringData3 = "$filler4"; $stringData4 = "$filler5"; $stringData5 = "$filler6"; $stringData6 = "$filler7"; $stringData7 = "$filler8"; $stringData8 = "$filler9"; $stringData9 = "$filler10"; $stringData10 = "$filler11"; $stringData11 = "$filler12"; $stringData12 = "$filler13"; When assigning a lot of HTML code use the HEREDOC syntax instead. So your new code cleaned up <?php $a1 = $_POST["PN"]; $a2 = $_POST["Date_name"]; $a3 = $_POST["Note"]; // Assign all HTML code within one single variable. // All variables within HERDOC will be parsed $stringData = <<<HTMLCODE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"; <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'> <title>$a1</title> </head> <body bgcolor="#FFFFFF" text="#000000"> <div id="bv_Text1" style="position:absolute;left:34px;top:34px;width:713px;height:32px;z-index:0;" align="left"> <font style="font-size:27px" color="#000000" face="Arial"><b>Note name:$a1</b></font></div> <div id="bv_Text2" style="position:absolute;left:12px;top:76px;width:781px;height:32px;z-index:1;" align="left"> <font style="font-size:27px" color="#000000" face="Arial"><b>Date and time:$a2</b></font></div> <textarea name="TextArea1" id="TextArea1" style="position:absolute;left:41px;top:134px;width:743px;height:391px;font-family:Courier New;font-size:16px;z-index:2" rows="20" cols="70" readonly="readonly">$a3</textarea> </body> </html> HTMLCODE; $myFile = "notenw.html"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, "\n"); fwrite($fh, "$stringData\n"); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187842-undefined-variables/#findComment-991838 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.