Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submit onChange

Status
Not open for further replies.
May 9, 2000
446
GB
Hi, I asked this question the other day and got a response using the onbeforeunload but I need some more help! My users need to be warned to submit their data b4 leaving the current screen. However I only want this warning to show if they have changed any of the text boxes / drop downs on the current screen. I guess i could use onchange but I'm lost!!!

Any help greatly appreciated
 
Every textbox, dropdown, radio and checkbox you have to specify the onChange event.
Set a variable and in the body onbeforeunload you check if this variable has been changed.
If so alert the user.

here is some sample code:

<script>
Changed=false;
</script>
<body onbeforeunload=&quot;if(Changed){return 'you have to save your changes'}&quot;>

<form action=test.asp>
<select name=txt id=txt onchange=&quot;Changed=true&quot;>
<option value=1>lkjf
<option value=2>lkjf
<option value=3>lkjf
</select>
</form>
</body>
 
harmmeijer one quick question, if I want to stop the unbeforeload event / msg box appearing when users click on the submit button how would I do it??? I tried putting onchange=&quot;Changed=False&quot; but I get told that false isn't recognised...

Cheers
 
Hi vanillapod, i think i told you to use onbeforeunload. If you want to stop message box from appearing the in your form tag write onSubmit=&quot;Changed=true&quot;
Harmmeijer has put the event in the body tag, nothing wrong with that but I would declare the variable in the Head section of the web page and also put the event handler in there too ie

<head>
<script language=javascipt>
var Changed = false;
window.onbeforeunload = function() {if (!Changed) {event.returnValue = &quot;...&quot;;}}
</script>
</head>
<body>
....
<form ... onSubmit=&quot;Changed=true&quot;>
 
Like sjravee says:
<form ... onSubmit=&quot;Changed=false&quot;>
Should do it.


Javascript is case sensitive so Changed=False cannot work, it should be Changed=false.

Here is the full code:

<script>
Changed=false;
</script>
<body onbeforeunload=&quot;if(Changed){return 'you have to save your changes'}&quot;>

<form action=test.asp onSubmit=&quot;Changed=false&quot;>
<select name=txt id=txt onchange=&quot;Changed=true&quot;>
<option value=1>lkjf
<option value=2>lkjf
<option value=3>lkjf
</select>
<input type=submit value=&quot;send changes to server&quot;>
</form>
</body>
 
Nice one cheers for the help guys, I forgot javascript was case sensitive... thats a bit of a pain!

Anyway cheers again

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top