dcsimg
www.webdeveloper.com
Results 1 to 8 of 8

Thread: file upload resetting page elements to defaults

  1. #1
    Join Date
    Dec 2010
    Posts
    28

    file upload resetting page elements to defaults

    I have used the following code to upload a file and display its contents in a text area however when i click upload file, it not only uploads the file and shows the contents but it also resets all other elements on the page to the default losing any values entered or any radio buttons set.

    Is there a way to stop the upload form refreshing the entire page and setting all other elements back to the defaults?



    <form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    <textarea name="Values"><?php
    if (!empty($_FILES['uploadedfile']) && file_exists($_FILES['uploaded_file']['tmp_name'])) {
    echo htmlentities(file_get_contents($_FILES['uploadedfile']['tmp_name']) , ENT_QUOTES, 'UTF-8');
    }
    ?></textarea>

  2. #2
    Join Date
    Apr 2010
    Posts
    227
    Not at all since your doing a post.

    Easiest solution is to store the values in a cookie using javascript. Then on page reload re-enter in the values.

    HTML Code:
    onSubmit="storevalues();"
    Last edited by Belrick; 02-22-2011 at 11:50 PM.

  3. #3
    Join Date
    Dec 2010
    Posts
    28
    Im new to php so I dont really unsertand.....how to I make something happen on page reload?

  4. #4
    Join Date
    Apr 2010
    Posts
    227
    The code i gave you will run the JS cookie script that stores the values. (probably best if they had a short expiry time)

    Then on page load you will check to see if the cookie exists and if it does load the values from them into the form.

    HTML Code:
    <body onload='checkcookie()'>

  5. #5
    Join Date
    Dec 2010
    Posts
    28
    you said 'on page load you will check to see if the cookie exists' Where would i put the code to check if the cookie exists? I dont know where in the code it re loads the page.

  6. #6
    Join Date
    Apr 2010
    Posts
    227
    Quote Originally Posted by claireneedshelp View Post
    you said 'on page load you will check to see if the cookie exists' Where would i put the code to check if the cookie exists? I dont know where in the code it re loads the page.
    This is all client side behaviour.

    HTML + Javascript.

    Cookie scripts are a dime a dozen.
    Previously i gave you the html+js code that runs a js script on a page load.

    Below is a rough js cookie code block. Im not trying to muck you around im just honestly restrained by two things.

    1. I cannot be bothered writting the entire code for you
    2. You wont learn much if i did anyway.

    So im trying to stear you in the right direction and obviously failing miserably.

    HTML Code:
    
    <script type="text/javascript">
    
    function checkcookie(){
    someformdata = getCookie('someformdata');
    if (someformdata != null && someformdata != ""){
    //cookie exists now use the cookie data to load forms.
    document.forms.formname.inputname.value = someformdata;
    
    }
    }
    
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
    }
    
    </script>
    Last edited by Belrick; 02-26-2011 at 02:54 PM.

  7. #7
    Join Date
    Dec 2010
    Posts
    28
    thank you for your time and code.....what Ive been trying to find out...maybe not worded clearly is.....

    where would I call the function checkcookie() ?

  8. #8
    Join Date
    Apr 2010
    Posts
    227
    *blink*

    I gave you the location already when i provided the code.

    HTML Code:
    <body onload='checkcookie()'>
    Its a client side action.

    It goes in the body tag of your page.
    All of your problem solutions is client side and not PHP

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles