For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > July 2004 > help with editing file in texarea









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help with editing file in texarea
Wayne...

2004-07-21, 8:56 pm

ok what i am trying to do is a very rough text editor so config files can be
edited. I can get it to read and write but just not after the content has
been edited. I've tried farting around with it but I guess its something
simple I am overlooking. the code is below {i'll leave out the functions etc
that dont matter]
thanks in advance

Wayne...
-------------------
using get the name of the file is passed from a previouis page
-------------------

<?php require_once('safeshowsource.php'); ?>//lists only the source of
configs allowed

function write_file($filename, $filecontents)
{
$handle= fopen($filename,'w+');
fputs($handle, $filecontents);
fclose($handle);
}
?>
<?
function read_file($file)
{
$handle= fopen($file,'r');
$contentsfile = fread($handle, filesize($file));
return $contentsfile;
fclose($handle);
}
?>
<?

$url = $_GET['url'];
echo safeshowsource($url); //to test the source views correctly
$urlname = "test.txt"; // a test file I write to to stop messing up the
configs during editing
$contents = (read_file($url));
?>
<h2 align="center">Edit your document the press 'Edit' to save</h2>
<? echo "<h3>file name: ", $url, "</h3><br>"; ?>
<form action="" method="POST" name="form1" onSubmit='<? write_file($urlname,
$contents); ?>'>
<p><textarea name="content" cols="100" rows="15" id="content"><? echo
$contents;?></textarea></p>

<? $contents = "content"; // no idea why I put this here grasping at straws
I think!! ?>
<p><input type="submit" name="Submit" value="Edit"></p>
</form>
===================
any ideas why the file doesn't update after the write and only writes the
original content? answers on a postcard please :-P


Andy Barfield

2004-07-21, 8:56 pm

Wayne... wrote:
> <form action="" method="POST" name="form1" onSubmit='<? write_file($urlname,
> $contents); ?>'>

I have not seen this done before - I think that you may have to POST to
PHP_SELF in order to get the values through - I'm not 100% sure though.
I'm gonna make an assumption that the $_POST array may not be populated.

Also, if you are running with register_globals off, you will need to
address $contents through the $_POST superglobal.


> any ideas why the file doesn't update after the write and only writes the
> original content? answers on a postcard please :-P

Does the webserver have permission to write to the file? Are you getting
any errors returned? Check the time stamp to see if the file has been
updated, rather than just being left untouched.

Also, please do not multipost - it's ok to post the same message to
several groups (crossposting) but you have also posted this message to
another group with a different subject line, that means that others will
not see replys across newsgroups and that people will end up reading the
same message twice.

HTH, regards,

Andy
Wayne...

2004-07-21, 8:56 pm

thanks for taking the time to reply firstly.
I'll explain the steps that the user takes and exactly what happens to clear
up as much as i can.

1 user arrives at index page and enters name of file they wish to edit
2 at the source page [code I supplied] they edit the file as neeeded and hit
the save button
3 this writes to a file [once stable it will overwrite the existing one]
4 what actaully happens is it writes teh pre edited content and not teh
edited version from teh text area.

I'm runing this on my test box at the mo so i know I have total control over
the globals etc....I just cant see why it will write to the test.txt file
the original contents only [to me this proves the permissions are set ok,
plus I triple checked them].
any ideas?
I can supply the full source I m using if needed..... I'm pulling my hair
out with this....lol
thanks again

Wayne...


Andy Barfield

2004-07-22, 3:55 am

Wayne... wrote:
> 4 what actaully happens is it writes teh pre edited content and not teh
> edited version from teh text area.

Are you absolutely sure that it is re-writing and not just leaving the
file untouched?

My own gut feeling is that this will not work...
<form action="" method="POST" name="form1" onSubmit='<?
write_file($urlname, $contents); ?>'>

The POST data is sent to the server and becomes available to the next
page, so the string $content here:

<p><textarea name="content" cols="100" rows="15" id="content"><? echo
$contents;?></textarea></p>

is not changed by the script until it is posted - you have never
re-assigned the new value to the variable. Don't forget thet PHP is
server-side, not client side, so until you send an HTPP request to the
server, it knows nothing.

Also this may confuse - id="content" - you later refer to the value as
$contents

Try changing the form tag to actualy post the data - like this
<form action='<? echo $_SERVER['PHP_SELF'] ?>' method="POST" name="form1">

Don't forget that you will need $url available to the script, so you
will have to post it back as a hidden field and retrieve it with $_POST
rather than $_GET

Good luck, Andy
Wayne...

2004-07-22, 3:56 pm

ok I finally managed to get it working the easiest way was to split it into
2 files [one to display and let the user edit teh file and then forward it
to a 2nd where it was saved]. All is now working and thanks a million for
all the help. Took a while but tunred out the contents of the hidden text
area that contained some of the vairiables were not passing correctly., so
after some debugging teh thing now works :)
thanks alot Andy

Wayne...


Andy Barfield

2004-07-22, 3:56 pm

Wayne... wrote:

> ok I finally managed to get it working the easiest way was to split it into
> 2 files [one to display and let the user edit teh file and then forward it
> to a 2nd where it was saved]. All is now working and thanks a million for
> all the help. Took a while but tunred out the contents of the hidden text
> area that contained some of the vairiables were not passing correctly., so
> after some debugging teh thing now works :)
> thanks alot Andy


You're welcome, pleased you got it sorted.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com