Results 1 to 9 of 9

Thread: Save/Restore form fields using *.txt files

  1. #1
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Save/Restore form fields using *.txt files

    How would I save/restore form fields using a text file?

    Basically,

    Code:
    [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?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    here's a link that might be helpful--
    http://www.dynamicdrive.com/forums/s...ead.php?t=9940
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2006
    Location
    New York City
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    How about this?

    Code:
    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

    Code:
    $contents = get_file_contents("user.txt");
    $break = explode("|||", $contents);
    list($formfield1, $formfield2, $formfield3) = $break;
    then you can put in the html:

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

    Hope that helped.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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(25015);
    $bg imagecolorallocate($im255255255);
    $textcolor imagecolorallocate($im000);
    imagestring($im400$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)
    ?>
    Last edited by tech_support; 01-25-2007 at 06:50 AM.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Ideas, anyone?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Nobody?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    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:
    PHP Code:
    $contents "\n".$_GET['field'].'='.$_GET['value']; 
    - Mike

  9. #9
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Thanks, but I already worked out the problem.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •