PDA

View Full Version : Save/Restore form fields using *.txt files



tech_support
01-17-2007, 04:32 AM
How would I save/restore form fields using a text file?

Basically,




[FormName]

FormField=User entered data



and saved onto a login name, eg. "User.txt" and restored next time the user logs in. And also, the user should have a "Save" button to save all form data.

Any ideas for this?

djr33
01-17-2007, 04:35 AM
here's a link that might be helpful--
http://www.dynamicdrive.com/forums/showthread.php?t=9940

kosi
01-17-2007, 08:49 PM
How about this?



function createFile($newfile, $text) {
if (!file_exists($newfile)) {

$fp = fopen ($newfile, "wb");
fwrite($fp, $text);
chmod($newfile, 0666);
fclose ($fp);

return;
}
}

$text = "$formfield1 ||| $formfield2 ||| formfield3";
createFile("user.txt", $text);

and then


$contents = get_file_contents("user.txt");
$break = explode("|||", $contents);
list($formfield1, $formfield2, $formfield3) = $break;

then you can put in the html:


<input type='whatever' name='formfield1' value='<?php echo($formfield1); ?>' />

etc.

Hope that helped.

djr33
01-17-2007, 10:17 PM
That is certainly along the lines of what he is looking for, though it would need specific changes to fit the exact purpose.
(I was talking to him on IM, and said he should probably post it, just to get some more input; large project, as he said he needed 300 fields to be part of this.)

The code above looks good, except that $_POST[] must be used to get the data from the form in the first place (unless using ajax, which is a different story).

Also, seems like the file SHOULd be overwritten if it exists because the contents are updating themselves each time the form page is accessed.

tech_support
01-25-2007, 06:42 AM
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
/*
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&#37;;
}
</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
/*
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
/*
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)
?>

tech_support
01-27-2007, 04:12 AM
Ideas, anyone?

tech_support
01-28-2007, 05:16 AM
Nobody?

mburt
01-28-2007, 12:48 PM
When you put a line break into a textarea, and restore it, it won't use line breaks

$fields[$part[0]] = htmlentities(stripslashes($part[1]));
stripslashes removes any slashes include linebreaks. The browser reads line breaks like: \n, so logically it will be removed as anything else would (\\, '\, etc.)


When you save the new data, it doesn't overwrite it, it shoves it aside (Try it)

$contents .= "\n".$_GET['field'].'='.$_GET['value'];

When you assign a variable you just write "=", if you want to add to it (in js it would be +=) you write ".=".
Try replacing $contents with this:

$contents = "\n".$_GET['field'].'='.$_GET['value'];

tech_support
01-30-2007, 04:59 AM
Thanks, but I already worked out the problem.