0

I have a registration form, but when the page refreshes the data is lost and the user has to enter the data again. I want the data to remain there even if the page refreshes.

I am using the serialize function to serialize the form data but now how should I print the stored values when page is refreshed?

setcookie('formdata', serialize($formdata), time()+30*24*60*60);

But it is saving the data at the end. I want the data to be stored as soon as the user enters the value and if the page refreshes and the field was filled it should populate the data.

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
user3058875
  • 59
  • 1
  • 13
  • you can use session for it.... – Dinesh Jan 08 '14 at 07:26
  • If the page just refreshed or if your form is submitted, in case it has errors the form data wouldn't be lost? Is that what you're trying to do? – Ilia Ross Jan 08 '14 at 07:26
  • I dont want to store the data on server I want to store on the client side only – user3058875 Jan 08 '14 at 07:27
  • What are you trying to achieve? Save data on form submit or save data everytime a user enters something in the input field? – Ilia Ross Jan 08 '14 at 07:28
  • You say `registration form` and you say `but when the page refreshes the data is lost`? What do you mean refreshed? When the form in submitted - then it's lost as it has errors in it and a user has to again stay with registration form? – Ilia Ross Jan 08 '14 at 07:30
  • @ Ilia Rostovtsev : I want to save data everytime a user enters something in the input field, so that if the page get refreshed/connection is lost.and he open the form the data which he filled previously should be there until the user submits the form. – user3058875 Jan 08 '14 at 07:44
  • Yes, you can do it using cookies on client-side! – Ilia Ross Jan 08 '14 at 08:33

2 Answers2

0

Instead of saving the cookies with PHP, you do need to do it with Javascript / JQuery.
You can do it everytime the user types on an input box (.change).

Have a look at: How do I set/unset cookie with jQuery?

Community
  • 1
  • 1
zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • but if i put setCookie(key, value) again each field in form and check if its set then print the value while the form loads will it not work fine for me – user3058875 Jan 08 '14 at 07:24
  • use `$.cookie(cookie,value)` to save the cookie with Javascript. Then, when loading the form page, print the value of the cookie inside the input boxes with PHP. `$_['COOKIE']` – zurfyx Jan 08 '14 at 14:35
0

you can use javascript to save form values into cookies. use jquery $.cookie function on change event. also you can use javascript without jquery

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else
        var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

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 "";
}

just use these functions to store and load cookie by name.

gogagubi
  • 965
  • 1
  • 15
  • 36