Forum Moderators: coopster

Message Too Old, No Replies

retaining data in a form

         

bablu

7:24 am on Apr 10, 2011 (gmt 0)

10+ Year Member



we have first 3 textboxes........as below

1.lesson name

2.lesson date

3.materials

now below is the link for upload a file...if we go for upload a file,the data entered in a textboxes is lost when we come back from uploading a file....how the data is maintained in a form?please help...........thanks in advance...

wyweb

10:03 am on Apr 10, 2011 (gmt 0)



I've used cookies to do that. Give me a minute and I'll find you the code.

Do you object to using cookies?

bablu

10:14 am on Apr 10, 2011 (gmt 0)

10+ Year Member



yes...give me...thanks..

wyweb

10:19 am on Apr 10, 2011 (gmt 0)



Part #1

Place this in an external javascript and call it to the page in the head:


<!-- Begin
// Cookie Functions //////////////////// (:)

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);

// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');

var expDays = 100;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

// cookieForms saves form content of a page.

// use the following code to call it:
// <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

// It works on text fields and dropdowns in IE 5+
// It only works on text fields in Netscape 4.5


function cookieForms() {
var mode = cookieForms.arguments[0];

for(f=1; f<cookieForms.arguments.length; f++) {
formName = cookieForms.arguments[f];

if(mode == 'open') {
cookieValue = GetCookie('saved_'+formName);
if(cookieValue != null) {
var cookieArray = cookieValue.split('#cf#');

if(cookieArray.length == document[formName].elements.length) {
for(i=0; i<document[formName].elements.length; i++) {

if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
}
}
}
}

if(mode == 'save') {
cookieValue = '';
for(i=0; i<document[formName].elements.length; i++) {
fieldType = document[formName].elements[i].type;

if(fieldType == 'password') { passValue = ''; }
else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
else { passValue = document[formName].elements[i].value; }

cookieValue = cookieValue + passValue + '#cf#';
}
cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter

SetCookie('saved_'+formName, cookieValue, exp);
}
}
}
// End -->

wyweb

10:31 am on Apr 10, 2011 (gmt 0)



Part #2

In your opening body tag, add the following:

<body onload="cookieForms('open', 'deletion')" onunload="cookieForms('save', 'deletion')">


Part #3

Name your form "deletion." It's just as easy to show you what mine looks like I guess:

<form method=$request_method action="http://www.example.com/cgi-bin/admin.cgi" name="deletion" onsubmit="return ConfirmDelete()" target="_blank">


Part #4

Put a hidden input in your form that looks like this:

<input type="hidden" name="terminate" value="on">


Part #5

Other inputs will need to look like this:

<input class="smtext" type="text" name="start_date" size="10" OnBlur="FormatDeleteTime(document.Deletion.start_date.value)"> 


Do all of this and do it effectively and your form data will stick. It will stick quite well. In fact you may want to edit cookie duration to something smaller than 100 days if you have repeat visitors. It can be quite annoying.

wyweb

12:44 pm on Apr 10, 2011 (gmt 0)



I'm sure it's just an oversight on your part but it's customary to thank someone when they've just spent their own time to give you instruction on something you didn't have a single clue about.

You're welcome anyway.

wyweb

1:09 pm on Apr 10, 2011 (gmt 0)



yes...give me...thanks..


And furthermore you don't need to thank people in advance. Wait til they do something for you, then thank them.

And learn the word "please." Saying "give me" almost made me pass this one up. Give me please works a whole lot better brother.

And I'm not insensitive to language and cultural differences either. You're not on your home court now though. Learn the etiquette.

Mods you can delete this if I'm going to far. I suspect I am. I just lost an hour of work to help this this one-hit wonder out and I'm slightly pissed that he can't even acknowledge the time I spent.

I bill at 50.00 an hour.

Matthew1980

5:31 pm on Apr 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@wyweb: We appreciate your efforts :)

I know nothing about Java Script either and I don't mean to tread on toes with this post :)

Personally though I would have opted of just using the $_POST globals that were already set at this point, let the server do the work for you, then your not reliant on the clients workstation having cookies enabled.

<input name="FirstName" type="text" id="FirstName" value="<?php echo (isset($_POST['FirstName']) ? strip_tags($_POST['FirstName']) : ''); ?>" />

Unless I misunderstand the context of this issue, this should do the trick too.

This is just one of may options that's available to this issue.

Cheers,
MRb

wyweb

10:10 pm on Apr 10, 2011 (gmt 0)



Well, that's much simpler Matthew, and you're certainly not treading on my toes. You're offering an alternative and those are important to have.

I'm fairly fluent in JavaScript. Fairly anyway. You can take what I know about PHP, stick it in a glass and still have room to pour a full beer in.

And you're right, your method doesn't rely on clientside cookies. Lots of people are wierded out about cookies these days. I get this on the phone all the time. 'What's this thing about cookies. How dangerous are they really?' And then I launch into my 'cookies are your friend' routine.

I use cookies extensively. I have disclaimers on all my sites explaining that portions of this site may not work as you hope it will unless you have cookies enabled, and then I have a link that explains what cookies are and what their behavior is. What they can do and what they can not do. I don't put them on your box forever either. Most of mine expire in 7 days, if not sooner. Some of them only last the session.

I'd lose a lot of time if I cleared cookies right now. In fact I won't do it unless it's absolutely necessary and fortunately that situation has only happened twice in the past 11 years.

I was going with what I knew. What I knew worked and what I had used successfully in the past. It was Sunday morning and I didn't figure anybody else was gonna get to this. And I did try to keep it simple for Mr. Bobaloo. As simple as I could anyway. I felt he probably needed that and I still think he does.

wyweb

1:43 am on Apr 11, 2011 (gmt 0)



That's why I asked him if he objected to cookies.

Some do.

He doesn't have a clue what he objects to.

rocknbil

3:58 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This was answered last month, did you misplace it?

multiple options for maintaining form state [webmasterworld.com]

wyweb

5:12 pm on Apr 11, 2011 (gmt 0)



No, I didn't misplace anything. I'm just pissed the guy won't say thank you.

Who are you talking to?

Matthew1980

6:12 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>This was answered last month, did you misplace it?

Never realised that Rocknbil; thanks for pointing this out to us.

We shall await an outcome then.. Comical that I offered the same solution to both threads though..

Cheers,
MRb

wyweb

6:15 pm on Apr 11, 2011 (gmt 0)



Matthew see your PM's

To get back on topic. Maybe I did misplace it.

I do it all the time here at the house.

rocknbil

9:59 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Refer to the OP of this thread and the poster in the link, they are the same and this is the same question.

wyweb

1:56 am on Apr 12, 2011 (gmt 0)



Agreed