dmikester1 Posted September 18, 2007 Share Posted September 18, 2007 I am working on a simple online file editor. I want it to pop up a message when the user hits "save" with no filename. But my PHP script is refreshing the page so the message is shown for a split-second and disappears. Can anyone tell me how to keep PHP from refreshing my page? Here is the URL: http://2007.rockip.com/MikesFileEditor.php And here is the code: <? echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang= "en"> <head> <title>Simple File Editor</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <script type="text/javascript"> //<![CDATA[ <!-- function checkIfEmpty(filename) { if(filename == "") { var message = "Please type a name for the file(including extension)"; popupText(message); } } function popupText(message) { document.getElementById('hidden').innerHTML = message; document.getElementById('hidden').style.display = "inline"; } --> //]]> </script> </head> <body> <?php $filedir = "."; if(is_readable($filedir)) { $file=".htaccess"; ?> <h2>Currently editing <span class="red">.htaccess</span></h2> <h2>in <span class="red"><?php echo getcwd(); ?></span></h2> <form action="<?=$PHP_SELF?>" method="post"> <div align="center"> <textarea rows="30" cols="80" style="border: 1px solid #666666;" name="updatedfile"> <?php if (!file_exists($file)) { $file2open = fopen($file, "w"); } else { $file2open = fopen($file, "r"); } $current_data = @fread($file2open, filesize($file)); $current_data = eregi_replace("</textarea>", "<END-TA-DO-NOT-EDIT>", $current_data); //Echo the data from the file echo $current_data; //Close the file fclose($file2open); ?> </textarea></div> <br /> <div id="center">Name of file: <input type="text" name="filename" id="filename" /><div id="hidden">text</div><br /><br /> <input type="submit" name="save" id="save" value="Save Changes" onclick="checkIfEmpty(document.getElementById('filename').value)"> <?php if (isset($_POST['save'])) { if (is_writable($file)) { $data_to_save = $_POST["updatedfile"]; $data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save); $file2save = fopen($file, "w+"); if (fwrite($file2save,$data_to_save)) { echo "<meta http-equiv='refresh' content='0;URL=MikesFileEditor.php'>"; echo "file saved!"; //Close file fclose($file2save); } else { //If write fails show failure page echo "file not saved!"; //Close file fclose($file2save); } } } } ?> </input> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/ Share on other sites More sharing options...
cooldude832 Posted September 18, 2007 Share Posted September 18, 2007 simple solution is use javascript if that field is empty make it give an alert button Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-350322 Share on other sites More sharing options...
dmikester1 Posted September 18, 2007 Author Share Posted September 18, 2007 OK, is there any way to just have js show a message on the page and stay there? Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-350400 Share on other sites More sharing options...
AV1611 Posted September 18, 2007 Share Posted September 18, 2007 you might make do it like this: let them form submit, and put clauses to validate each entry, and then push them back to the form, with the valid fields already filled out and the invalid ones with a little line of red text stating they need to fill out that field again... all done without JS and no alert... (I hate js and alerts) Anyways, it looks nice and professional Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-350420 Share on other sites More sharing options...
cooldude832 Posted September 18, 2007 Share Posted September 18, 2007 your trying to disprove the use of js and instead sub in a language that isn't designed for that. Javascript by design is for real time scripting, giving response without recontacting the server. PHP is Prehypertext you are looking for a posthyerptext solution I.e javascript Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-350505 Share on other sites More sharing options...
AV1611 Posted September 23, 2007 Share Posted September 23, 2007 I know, I agree, and i still hate js. (I would have an easier time if I learned it, but I can't seem to get any "traction" with it, so I just find other ways of getting the job done...) Oh, and some people disable it in their browser, so that's a problem anyways. Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-353450 Share on other sites More sharing options...
cooldude832 Posted September 23, 2007 Share Posted September 23, 2007 if its disabled then they can't use your software that simple, this isn't a general public application so you can set rules to use it. Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-353452 Share on other sites More sharing options...
AV1611 Posted September 23, 2007 Share Posted September 23, 2007 I wasn't trying to start a debate I was just offering a possible solution based on my abilities I did not mean to imply that using JS was inappropriate. Quote Link to comment https://forums.phpfreaks.com/topic/69714-php-page-refreshing/#findComment-353460 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.