dmikester1 Posted February 6, 2008 Share Posted February 6, 2008 I have a directory/file listing on my site. I can click "edit" next to each of the files to pop up a basic file editor which loads in the text of the file using file_get_contents($newFile); Now, I have a save button at the bottom and when I click it, it saves the edited text to the current file being worked on. Then it pops up a message at the bottom saying "file saved" with javascript. The problem is, when I post, it reloads the window loading in the unedited file contents. Here is my form code: <form method="post" action="<?php echo $_SERVER['PHP_SELF']."?dir=".$_GET['dir']."&file=".$_GET['file'];?>"> <div><textarea rows="25" cols="70" style="border: 1px solid #666666;" id="updatedfile" name="updatedfile"> <?php $filedir = $_GET['dir']; if($filedir == "") { $filedir = "."; } if(is_readable($filedir)) //check that the get "value" is readable { $newFile = $_GET['file']; if ($newFile == "ht") { $newFile = ".htaccess"; $fileText = file_get_contents($newFile); echo $fileText; ?></textarea><br /></div> <div class = "center">File will be saved as: <strong>.htaccess</strong> <?php } else if($newFile == "") { ?></textarea></div> <div class = "center">Name of file: <input type="text" name="filename" id="filename"> <?php } else { $fileText = file_get_contents($newFile); echo $fileText; ?></textarea><br /></div> <div class = "center">File will be saved as: <strong><?php echo $newFile ?></strong> <?php } ?> <br /><br /></div> <div id="hidden"></div> <div><input type="submit" name="save" id="save" value="Save Changes" onclick="return checkIfEmpty('filename')"></div> <?php if (isset($_POST['save'])) { //if the "save" button was clicked if($newFile == "") { $file = $_POST['filename']; } else { $file = $newFile; } $file = $cwd.$file; $file2save = fopen($file, "w+"); if (is_writable($file)) { $data_to_save = $_POST["updatedfile"]; $data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save); if (fwrite($file2save,$data_to_save)) { ?><script type="text/javascript"> document.getElementById('updatedfile').focus(); </script> <?php echo "file saved!"; //Close file fclose($file2save); }else { //If write fails show failure page echo "file not saved!"; //Close file fclose($file2save); } }else { echo "not writable!"; } } } ?> </form> Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/89765-loading-file-contents-in-to-window-on-submitpost/ Share on other sites More sharing options...
dmikester1 Posted February 8, 2008 Author Share Posted February 8, 2008 Anyone have any ideas about this one? Thanks Mike Quote Link to comment https://forums.phpfreaks.com/topic/89765-loading-file-contents-in-to-window-on-submitpost/#findComment-461904 Share on other sites More sharing options...
GingerRobot Posted February 8, 2008 Share Posted February 8, 2008 Im not 100% sure i've fully understood but what i think is happening is: 1.) You edit the file 2.) You click save 3.) You get the correct message saying it has saved AND if you view the file again it has indeed been saved successfully 4.) BUT along with the saved message, you see the old code If thats about right, then it's because you're doing the saving at the bottom of your script, after the contents is grabbed from the file and displayed. You need to be doing the form processing at the top. Quote Link to comment https://forums.phpfreaks.com/topic/89765-loading-file-contents-in-to-window-on-submitpost/#findComment-461995 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.