User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 430,115 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,265 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1889 | Replies: 3
Reply
Join Date: Aug 2007
Posts: 57
Reputation: rrocket is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rrocket's Avatar
rrocket rrocket is offline Offline
Junior Poster in Training

Help Textboxs not staying disabled after browser back button clicked

  #1  
Jan 29th, 2008
I have a page with radio buttons that either enable or disable textboxes depending on the radio button selection. When the user clicks submit the they are taken to a thank you page, but if they hit the back button on the browser the radio buttons both return false and the correct boxes are not disabled... Is there a way to trigger a function or store what boxes are diabled?

I tried clearing everything just before the redirect as a quick fix, but that did not help.

I am using this with .NET 2.0 C# if that makes a difference.

In a nutshell, I either need to make sure the form is cleared or the correct textboxes are disabled when the user hits the back button on the browser.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Location: Brownsville or Austin, TX or Faber, VA
Posts: 59
Reputation: world_weapon is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 2
world_weapon's Avatar
world_weapon world_weapon is offline Offline
Junior Poster in Training

Re: Textboxs not staying disabled after browser back button clicked

  #2  
Jan 29th, 2008
You could use a hidden form and javascript onchange submit to allow the code to assign the values to the cookie session and code the page to check the session for checked value. Maybe there is an easier way..... Let me think about it some more.....
The purpose of my existence is why I am here.
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Textboxs not staying disabled after browser back button clicked

  #3  
Jan 31st, 2008
You can have JavaScript save cookies. This is done immediately, so page reloads are not needed.

Example JS:

/**
* Set a cookie
* @param string cookie name
* @param string cookie value
* @param string cookie expiration counter in days
* @param string cookie path
* @param string cookie domain
* @param bool secure?
*/
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

/**
* Get a cookie value
* @param string cookie name
*/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/**
* Remebers form inputs after you fill them in
* @param string ID of the form you want inputs remembered for
*/
function rememberFormInputs(form_id) {
	// get a reference to the form element with id 'form_test'
	var form = document.getElementById(form_id);
	// get all child input elements of the form
	var els = document.getElementsByTagName('input');
	// iterate through all form child input elements
	for (var i = 0; i < els.length; i++) {
		// this is the element with index of i
		var el = els.item(i);
		// make sure this is a text input field
		if (el.type == 'text') {
		
			// event handler to catch onblur events
			// it sets the cookie values each time you move out of an input field
			el.onblur = function() {
				// this is the name of the input field
				var name = this.name;
				// this is the value of the input field
				var value = this.value;
				// set the cookie
				setCookie( form_id + name, value);
				alert('setCookie: '+name + ' = '+value);
			};
			
			// this inserts all the remembered cookie values into the input fields
			var old_value = getCookie(form_id + el.name);
			if (old_value && old_value != '') {
				alert('old value remembered: '+old_value);
				el.value = old_value;
			}
		}
	}
}

You can then give your form an ID, and pass the ID to rememberFormInputs(form_id, prefix) when the window has finished loading:

<form id="myform">.... inputs ....</form>

// function will be run after window/document loads
window.onload = function() {
	rememberFormInputs('myform');
}

Here is a demo that uses this:
http://demo.fijiwebdesign.com/exampl...rm_fields.html

View the source to see the code and how it works.

Note: this currently does not support radio buttons, but you can easily create function that does in the same manner.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Textboxs not staying disabled after browser back button clicked

  #4  
Jan 31st, 2008
Just thinking about it, a better way to do this would be to serialize the form inputs into a string. MooTools has a function that does this. I think its $('form_id').toQueryString();

Then save this to a cookie on the window.onbeforeunload event.

On the window.onload event, repopulate forms, or do your disabling etc. by reading the names of form elements and matching their values with the serialized string saved in the cookie.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 3:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC