Well, basically djr33 and I made up this code for saving/restoring fields using images.
Just two issues that we need to fix (and need help):
- When you put a line break into a textarea, and restore it, it won't use line breaks
- When you save the new data, it doesn't overwrite it, it shoves it aside (Try it)
index.php
PHP Code:
<?php
/*
Save/Restore Fields
Created by Daniel Ross and Peter Nguyen (Dynamic Drive Usernames: djr33 & tech_support)
Note: This credit must stay intact for leagal use
*/
session_start();
include("settings.inc.php");
$user.=".txt";
$loc = $folderloc."/".$user;
/*
Delete file contents
*/
if (isset($_GET['cmd']) && $_GET['cmd'] == "delete") {
$fp = fopen($user,"w");
fwrite($fp,"");
fclose($fp);
//header("location:index.php");
echo "File Contents Deleted";
}
if (isset($_GET['cmd']) && $_GET['cmd'] == "content") {
echo "<strong>File Contents</strong><br>";
echo nl2br(file_get_contents($loc));
}
/*
Restore Form Fields
*/
$parts = file($loc);
foreach ($parts as $part) {
$part = explode('=',$part);
$fields[$part[0]] = htmlentities(stripslashes($part[1]));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Save/Restore Form Fields</title>
<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
input {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
h1 {
font-family:Trebuchet MS;
font-size:26px;
}
img {border:0;}
#save {
position:absolute;
right:2px;
top:2px;
background-color:#FFFFCC;
font-size:10px;
display:none;
}
#file {
display:none;
}
.small {
font-size:60%;
}
</style>
</head>
<body>
<script type="text/javascript">
/*
Save/Restore Fields
Created by Daniel Ross and Peter Nguyen (Dynamic Drive Usernames: djr33 & tech_support)
Note: This credit must stay intact for leagal use
*/
//Configuration
var saveMsg = true //Show save message
var shTools = true //Show the tools
function save(sfield) {
//Setting all elements as variables
var tool_e = document.getElementById("tools")
var img_e = document.images["saveicon"]
var field = document.forms['form'].elements[sfield]
var stat = document.images['stat']
var d = new Date();
var locale = d.getTime();
//Checking if tools should be shown
if (shTools) tool_e.style.display = "block"
else tool_e.style.display = "none"
//Checking if the message should be shown
if (saveMsg) document.getElementById("save").style.display = "block"
//Saving data
stat.src = "create.php?time=" + locale + "&field=" + field.id + "&value=" + field.value
//Writing when it was last saved
document.getElementById("savestat").innerHTML = "Last saved: " + d.toLocaleString()
//Switching images
img_e.src="lsave.png"
/* DONE */
}
function deleteFile() {
if (confirm("Are you sure you want to delete all of the file contents?")) {
window.location = "?cmd=delete"
}
}
function showFile() {
window.location = "?cmd=content"
}
//Creating the image
if (document.createElement) {
var st=document.createElement('img');
st.setAttribute("id", "stat", 0);
st.setAttribute("name", "stat", 0);
st.setAttribute("alt", "Status", 0);
st.setAttribute("src", "create.php", 0);
document.body.appendChild(st);
}
else
document.write('<img src="create.php" alt="Status" name="stat" id="stat">')
</script>
<div id="save"><table><tr><td><img src="save.png" alt="Save" width="16" height="16" name="saveicon"></td>
<td><div id="savestat">Saving...</div><div id="tools">Tools: <a href="javascript:deleteFile()">Delete File Contents</a> | <a href="javascript:showFile()">Show File Contents</a></div></td>
</tr></table>
</div>
<h1>Form Field Save/Restore<span class="small"><em> using images B</em>eta</span></h1>
<form name="form" method="post" action="">
<p><label>Field 1:
<input name="test1" type="text" id="test1" value="<?php echo $fields['test1']; ?>" size="40" onChange="save(this.id)">
</label></p>
<p>
<label>Field 2:
<input name="test2" type="text" id="test2" value="<?php echo $fields['test2']; ?>" size="40" onChange="save(this.id)">
</label>
</p>
<p>
<label>Field 3:
<input name="test3" type="text" id="test3" value="<?php echo $fields['test3']; ?>" size="40" onChange="save(this.id)">
</label>
</p>
<p><label><input name="checkbox" type="checkbox" id="checkbox" value="checked" <?php echo $fields['checkbox']; ?> onChange="save(this.id)">
Sample checkbox</label>
</p>
<p>
<label>Sample textarea<br>
<textarea name="textarea" id="textarea" cols="80" rows="10" onKeyUp="save(this.id)"><?php echo $fields['textarea']; ?></textarea>
</label>
<a href="javascript:showFile()"></a> </p>
</form>
</body>
</html>
create.php
PHP Code:
<?php
/*
Save/Restore Fields
Created by Daniel Ross and Peter Nguyen (Dynamic Drive Usernames: djr33 & tech_support)
Note: This credit must stay intact for leagal use
*/
include("settings.inc.php");
$status = "Status: ";
if (isset($_GET['time'],$_GET['field'],$_GET['value'])) {
$user = 'user';
$file = $folderloc."/".$user.'.txt';
$contents = file_get_contents($file);
if (strpos($contents,$_GET['field'])) {
list($contents1,$contents2) = explode("\n".$_GET['field'],$contents,2);
list($contents2,$contents3) = explode("\n",$contents2,2);
$contents = $contents1."\n".$_GET['field'].'='.$_GET['value']."\n".$contents2;
}
else {
$contents .= "\n".$_GET['field'].'='.$_GET['value'];
}
$fp = fopen($file, "w");
fwrite($fp, $contents);
fclose($fp);
$status = "Saved at ".date("h:i:s");
}
else {
$status = "Insert data to update record.";
}
if (!showStatus) {
$status = "";
}
$im = imagecreate(250, 15);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 4, 0, 0, $status, $textcolor);
// output the image
header("Content-type: image/gif");
imagegif($im);
?>
settings.inc.php
PHP Code:
<?php
/*
Save/Restore Fields
Created by Daniel Ross and Peter Nguyen (Dynamic Drive Usernames: djr33 & tech_support)
Note: This credit must stay intact for leagal use
SETTINGS
*/
$user = "user"; //Username
$folderloc = "data"; //Data Folder
$showStatus = true; //Show the technical status (Appears in the image)
?>
Bookmarks