Jump to content

PHP page refreshing


dmikester1

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.