Reply to Re: question about variable's scope

Your name:

Reply:


Posted by Jerry Stuckle on 02/27/07 04:06

finer recliner wrote:
> i've been playing with the below code for a while now, and i cant get
> it to work correctly. the user should be able to input text into a
> textbox and upon hitting submit, it will append that text to whatever
> is in "newpage.html" and write this new chunk of text to data.txt.
>
> the final result of my code will be a bit more elaborate, but i can't
> even get this to work. i believe soemthing is wrong the scope of my
> variables or how my functions are written or something. it seems
> everytime i hit submit, only $text is written to data.txt. $above
> seems to be blank in the addHTML function. any suggestions?
>
> thanks!
> -dave
>
>
> /////////////////////////start code ////////////////////////////////
> <html>
> <head>
> <?php
> if ($submit) {
> $newdata = addHTML($newdata);
> $fp = fopen("data.txt", "w");
> fwrite($fp, $newdata);
> fclose($fp);
> }
>
> $fcurrent = fopen("newpage.html", "r");
> $i = 0;
> while(!feof($fcurrent)){
> $line = fgets($fcurrent, 4096);
> $above .= $line;
> }
> fclose($fcurrent);
>
> function addHTML($text){
> global $above;
> $newdata = $above . $text;
> return $newdata;
> }//end addHTML
>
> ?>
> </head>
> <body>
> <form action="<? print $PHP_SELF; ?>" method="post">
> <textarea name="newdata" rows="30" cols="80"></textarea>
> <br>
> <input type="submit" name="submit" value="Submit">
> </form>
> </body>
> </html>
> //////////////////////////////////end
> code//////////////////////////////////////////
>

In addition to Klarth's comments, you shouldn't use global variables.
If you would have passed $above to this function (as a second parameter)
your problem would have been more obvious (just one of the reasons not
to use globals).


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация